You will need to convert "a[(2,3,5),][mask]" into a single indexing expression, e.g, by using utility functions like np.nonzero() on mask. NumPy can't support assignment in chained indexing.
On Tue, Jun 19, 2018 at 1:25 PM Emil Sidky <si...@uchicago.edu> wrote: > 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: > In [72]: mask = randn(3,3)>0. > > In [73]: mask > Out[73]: > array([[ True, True, True], > [False, True, True], > [ True, False, True]], dtype=bool) > > Try to modify elements of "a" with the following line: > In [74]: a[(2,3,5),][mask] = 1. > 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: > In [76]: a[::2,][mask] = 1. > > 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 > expression like "a[(2,3,5),][mask]" ? > > Thanks , Emil > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion >
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion