Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm working on setting some boundary conditions for a water table model, and I am able to set the entire first row to a constant value, but not the entire first column. I am using np.zeros((11, 1001)) to make an empty matrix. Does anyone know why I am successful at defining the first row, but not the first column? I have noted the line in question 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))
like image 889
AF2k15 Avatar asked Jun 22 '15 21:06

AF2k15


2 Answers

All you have to do is to change

head[0][0:]

to

head[:, 0] = 16

If you want to change the first row you can just do:

head[0, :] = 16

EDIT:

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

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

Now we set in row 2 3 values 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 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 then also want to change certain values in e.g. two different rows 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.]])
like image 132
Cleb Avatar answered Sep 23 '22 21:09

Cleb


You can use a similar 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.]])
like image 22
Nitish Avatar answered Sep 24 '22 21:09

Nitish