0

I use boolean indexing to select elements from a numpy array as

x = y[t<tmax]

where t a numpy array with as many elements as y. My question is how can I do the same with 2D numpy arrays? I tried

x = y[t<tmax][t<tmax]

This does not seem to work however since it seems to select first the rows and then complains that the second selection has the wrong dimension.

IndexError: boolean index did not match indexed array along dimension 0; dimension is 50 but corresponding boolean dimension is 200
#

Here is an example

x1D = np.array([1,2,3], np.int32)
x2D = np.array([[1,2,3],[1,2,3],[1,2,3]], np.int32)
print(x1D[x1D<3]) --> [1 2]
print(x2D[x1D<3][x1D<3]) --> error

The second print statement produces an error similar to the error shown above. I use

print(x2D[x1D<3])

I get

[[1 2 3]
 [1 2 3]]

but I want

[[1 2]
 [1 2]]
3
  • 2
    y[t<tmax] works fine with 2D arrays too. Can you show with a sample what exactly isn't working? Commented Aug 31, 2019 at 20:13
  • What is t here? A 2D array as well? Commented Aug 31, 2019 at 20:13
  • Python processes each indexing operation [] independently. temp[t<tmax] followed by x=temp[t<tmax]. So each step has to make sense by itself. Commented Aug 31, 2019 at 20:31

1 Answer 1

1
In [28]: x1D = np.array([1,2,3], np.int32) 
    ...: x2D = np.array([[1,2,3],[1,2,3],[1,2,3]], np.int32) 

The 1d mask:

In [29]: x1D<3                                                                                               
Out[29]: array([ True,  True, False])

applied to the 1d array (same size):

In [30]: x1D[_]                                                                                              
Out[30]: array([1, 2], dtype=int32)

applied to the 2d it selects 2 rows:

In [31]: x2D[_29]                                                                                            
Out[31]: 
array([[1, 2, 3],
       [1, 2, 3]], dtype=int32)

It can be used again to select columns - but note the : place holder for the row index:

In [32]: _[:, _29]                                                                                           
Out[32]: 
array([[1, 2],
       [1, 2]], dtype=int32)

If we generate an indexing array from that mask, we can do the indexing with one step:

In [37]: idx = np.nonzero(x1D<3)                                                                             
In [38]: idx                                                                                                 
Out[38]: (array([0, 1]),)
In [39]: x2D[idx[0][:,None], idx[0]]                                                                         
Out[39]: 
array([[1, 2],
       [1, 2]], dtype=int32)

An alternate way of writing this '2d' indexing:

In [41]: x2D[ [[0],[1]], [[0,1]] ]                                                                           
Out[41]: 
array([[1, 2],
       [1, 2]], dtype=int32)

ix_ is a convenient tool for tweaking the indexing dimensions:

In [42]: x2D[np.ix_(idx[0], idx[0])]                                                                         
Out[42]: 
array([[1, 2],
       [1, 2]], dtype=int32)

Or passing the boolean mask to ix_:

In [44]: np.ix_(_29, _29)                                                                                    
Out[44]: 
(array([[0],
        [1]]), array([[0, 1]]))
In [45]: x2D[np.ix_(_29, _29)]                                                                               
Out[45]: 
array([[1, 2],
       [1, 2]], dtype=int32)

Writing In[32] so it's close to to your try:

In [46]: x2D[x1D<3][:, x1D<3]                                                                                
Out[46]: 
array([[1, 2],
       [1, 2]], dtype=int32)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.