How to set the first column to a constant value of an empty matrix np.zeros numPy?

I am working on setting some boundary conditions for a water table model, and I can set the entire first row to a constant value, but not the entire first column. I use np.zeros((11, 1001)) to create an empty matrix. Does anyone know why I am successfully defining the first row, but not the first column? I marked this line below.

 import numpy as np x = range(0, 110, 10) time = range(0, 5005, 5) xSize = len(x) timeSize = len(time) dx = 10 dt = 5 Sy = 0.1 k = 0.002 head = np.zeros((11, 1001)) head[0:][0] = 16 # sets the first row to 16 head[0][0:] = 16 # DOESN'T set the first column to 16 for t in time: for i in x[1:len(x)-1]: head[t+1][i] = head[t][i] + ((dt*k)/(2*Sy)) * (((head[t][i-1]**2) - (2*head[t][i]**2) + (head[t][i+1]**2)) / (dx**2)) 
+6
source share
2 answers

All you have to do is change

 head[0][0:] 

to

 head[:, 0] = 16 

If you want to change the first line you can simply do:

 head[0, :] = 16 

EDIT:

Just in case, you also wonder how you can change an arbitrary number of values ​​in an arbitrary row / column:

 myArray = np.zeros((6, 6)) 

Now we set line 2 3 to 16:

 myArray[2, 1:4] = 16. array([[ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 16., 16., 16., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.]]) 

The same thing works for columns:

 myArray[2:5, 4] = -4. array([[ 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 16., 16., 16., -4., 0.], [ 0., 0., 0., 0., -4., 0.], [ 0., 0., 0., 0., -4., 0.], [ 0., 0., 0., 0., 0., 0.]]) 

And if you also want to change certain values, for example. two different lines, at the same time you can do it like this:

 myArray[[0, 5], 0:3] = 10. array([[ 10., 10., 10., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0.], [ 0., 16., 16., 16., -4., 0.], [ 0., 0., 0., 0., -4., 0.], [ 0., 0., 0., 0., -4., 0.], [ 10., 10., 10., 0., 0., 0.]]) 
+4
source

You can use the same syntax.

 In [12]: head = np.zeros((11,101)) In [13]: head Out[13]: array([[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.]]) In [14]: head[:,0] = 42.0 In [15]: head Out[15]: array([[ 42., 0., 0., ..., 0., 0., 0.], [ 42., 0., 0., ..., 0., 0., 0.], [ 42., 0., 0., ..., 0., 0., 0.], ..., [ 42., 0., 0., ..., 0., 0., 0.], [ 42., 0., 0., ..., 0., 0., 0.], [ 42., 0., 0., ..., 0., 0., 0.]]) 
+1
source

Source: https://habr.com/ru/post/989557/


All Articles