get, access and modify element values for an numpy array
<p>I once saw the following code segment</p>

<pre><code>import numpy as np
nx=3
ny=3
label = np.ones((nx, ny))
mask=np.zeros((nx,ny),dtype=np.bool)
label[mask]=0
</code></pre>

<p>The <code>mask</code> generated is a bool array</p>

<pre><code>[[False False False]
 [False False False]
 [False False False]]
</code></pre>

<p>If I would like to assign some elements in mask to other values, for instance, I have been trying to use <code>mask[2,1]="True"</code>, but it did not work without changing the corrsponding entry as I expected.  What's the correct way to get access and change the value for an numpy array. In addition, what does <code>label[mask]=0</code> do? It seems to me that it tries to use each mask entry value to assign the corrsponding label entry value.</p>

<p>Here is a code snippet with some comments that might help you make sense of this. I would suggest you look into the link that @Divakar provided and look into <a href="https://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html#boolean-or-mask-index-arrays" rel="nofollow">boolean-indexing</a>.  </p>

<pre><code># a two dimensional array with random values
arr = np.random.random((5, 5))

# assign mask to a two dimensional array (same shape as arr)
# that has True for every element where the corresponding
# element in arr is greater than 0.5
mask = arr &gt; 0.5

# assign all the elements in arr that are greater than 0.5 to 0
arr[mask] = 0

# the above can be more concisely written as:
arr[arr&gt;0.5] = 0

# you can change the mask any way you want

# here I invert the mask
inv_mask = np.invert(mask)

# assign all the values in arr less than 0.5 to 1
arr[inv_mask] = 1
</code></pre>

python numpy scipy