Multiplying matrices of different sizes
I have a matrix full of zeros except for one zone, where I have values other than 0:

i=20
j=30
z = np.zeros((50,50))
while i &lt; 30:
    while j &lt; 40:
        z[j, i] = np.random.rand(1,1)
        j+=1

    j=30
    i+=1

matshow(z)
np.where(z == z.max())




Then I have another matrix, smaller than the first one. The distribution of values in this matrix follows a Gaussian curve:

import numpy as np
from scipy import signal

def gkern(kernlen=21, std=3):
    """Returns a 2D Gaussian kernel array."""
    gkern1d = signal.gaussian(kernlen, std=std).reshape(kernlen, 1)
    gkern2d = np.outer(gkern1d, gkern1d)
    return gkern2d
gkern(21, 3)




What I want is the following:

Identify where the maximum value of the 50x50 matrix is located. On this value, I want to center the maximum value of the smallest matrix, and then do the multiplication between the value of my small matrix and the values of the big matrices that are convered by the smaller matrix.

First use the original code to make the big matrix:  

i=20
j=30
z = np.zeros((50,50))
while i &lt; 30:
    while j &lt; 40:
        z[j, i] = np.random.rand(1,1)
        j+=1
    j=30
    i+=1


Then figure out where zmax is:

[x,y] = np.where(z==z.max());


Now we know where the matrix should be centered. Get the shape of the g_kern matrix. This will tell us the size of the submatrix that we need to construct by taking the values of our larger matrix "covered" by the shape of the g_kern matrix, centered around the max of the larger matrix

[a,b] = np.shape(g_kern_matrix)


Now we center the submatrix of the larger matrix at the position of z_max. Its shape is determined by the second smaller matrix:

#x range of larger matrix slice
[x_min,x_max] = [int(x-a/2),int(x+a/2)]
#y range of larger matrix slice
[y_min,y_max] = [int(y-b/2),int(y+b/2)]
#construct the submatrix of the larger matrix
submatrix = z[x_min:x_max,y_min:y_max]
#can take a look at the submatrix to see that it seems correct
plt.matshow(submatrix)
plt.show()


And now we can do the multiplication

#now element wise multiply(hadamard product) this with the gaussian curve matrix
z[x_min:x_max,y_min:y_max] = submatrix*g_kern_matrix
plt.matshow(z)
plt.show()


Here I have assumed that when you ask to multiply the g_kern matrix and the submatrix, you mean element wise multiplication, as you cannot multiply two nonsquare matrices of the same shape.
