[Numpy-discussion] question about array slicing and element assignment

Emil Sidky sidky at uchicago.edu
Tue Jun 19 13:51:12 EDT 2018

```Hello,
The following is an example where an array element assignment didn't work
as I expected.
Create a 6 x 3 matrix:

In [70]: a =  randn(6,3)

In [71]: a
Out[71]:
array([[ 1.73266816,  0.948849  ,  0.69188222],
[-0.61840161, -0.03449826,  0.15032552],
[ 0.4963306 ,  0.77028209, -0.63076396],
[-1.92273602, -1.03146536,  0.27744612],
[ 0.70736325,  1.54687964, -0.75573888],
[ 0.16316043, -0.34814532,  0.3683143 ]])

Create a 3x3 boolean array:

Out[73]:
array([[ True,  True,  True],
[False,  True,  True],
[ True, False,  True]], dtype=bool)

Try to modify elements of "a" with the following line:
No elements are changed in "a":
In [75]: a
Out[75]:
array([[ 1.73266816,  0.948849  ,  0.69188222],
[-0.61840161, -0.03449826,  0.15032552],
[ 0.4963306 ,  0.77028209, -0.63076396],
[-1.92273602, -1.03146536,  0.27744612],
[ 0.70736325,  1.54687964, -0.75573888],
[ 0.16316043, -0.34814532,  0.3683143 ]])

Instead try to modify elements of "a" with this line:

This time it works:
In [77]: a
Out[77]:
array([[ 1.        ,  1.        ,  1.        ],
[-0.61840161, -0.03449826,  0.15032552],
[ 0.4963306 ,  1.        ,  1.        ],
[-1.92273602, -1.03146536,  0.27744612],
[ 1.        ,  1.54687964,  1.        ],
[ 0.16316043, -0.34814532,  0.3683143 ]])

Is there a way where I can modify the elements of "a" selected by an