3

There is a 1D array of values:

arr0 = numpy.array([8,0,9,5])

There is another 2D array whose shape is (len(arr0),3):

arr1 = numpy.array([9,5,6],
                   [2,7,4],
                   [6,7,8],
                   [1,8,3])

I want to create a masked array of arr1 where arr1[i] is masked if arr0[i] == 0:

Result arr2 = [[9,5,6],
               [-,-,-],
               [6,7,8],
               [1,8,3]]

What is an elegant way to create this new masked array?

I know I can create it using a mask of shape (len(arr0),3). I am hoping I can create this using a mask of shape that is just (len(arr0)).

4

1 回答 1

1

arr0 == 0如果您执行以下操作,您的掩码可以由 bool 数组设置:

In [1]: arr1 = numpy.ma.masked_array(arr1)
In [2]: arr1[arr0 == 0] = numpy.ma.masked
In [3]: print arr1
[[9 5 6]
[-- -- --]
[6 7 8]
[1 8 3]]

(顺便说一句,您需要在 arr1 定义周围添加一组额外的括号。)

于 2013-04-26T06:03:45.257 回答