Skip to content
Advertisement

Create a matrix with np.random.normal

I need to create an nxn matrix in which the numbers in the cells are distributed following a Gaussian distribution. This code may not go well because it fills a cell with a sequence. how can I do?

mu, sigma = 8, 0.5 # mean and standard deviation

def KHead(nx, ny, mu, sigma):
KH0=np.zeros((nx,ny))
N=1000
for k in range(1,ny-1):
    for i in range(0,nx-1):
        KH0[(i,k)]= np.random.normal(mu, sigma, N )

return KH0

Advertisement

Answer

Edited for border of zeros

np.random.normal takes a size keyword argument. You can use it like this:

KH0 = np.zeros((nx, ny))
KH0[1:-1,1:-1] = np.random.normal(mu, sigma, (nx -2, ny - 2))
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement