0

I have this matrix of shape 10,000x30 in python. What I want is to find the indices of the rows, i.e., from the 10,000 rows, determine the indices with the 5th column value equal to 0.

How can I get the indices. Once I get the indices, I want to selected corresponding rows from another matrix B. How do I accomplish this?

horchler
  • 18,384
  • 4
  • 37
  • 73
user34790
  • 2,020
  • 7
  • 30
  • 37
  • 1
    To whoever suggested closing: I think the question is reasonably clear, especially if you know any Matlab or NumPy. However, a simple Google of "find matlab numpy" turned up... – horchler Oct 20 '13 at 21:15

1 Answers1

0
>>> a = np.random.randint(0, 10, (10, 5))
>>> a
array([[4, 9, 7, 2, 9],
       [1, 9, 5, 0, 8],
       [1, 7, 7, 8, 4],
       [6, 2, 1, 9, 6],
       [6, 2, 0, 0, 8],
       [5, 5, 8, 4, 5],
       [6, 8, 8, 8, 8],
       [2, 2, 3, 4, 3],
       [3, 6, 2, 1, 2],
       [6, 3, 2, 4, 0]])
>>> a[:, 4] == 0
array([False, False, False, False, False, False, False, False, False,  True], dtype=bool)
>>> b = np.random.rand(10, 5)
>>> b
array([[ 0.37363295,  0.96763033,  0.72892652,  0.77217485,  0.86549555],
       [ 0.83041897,  0.35277681,  0.13011611,  0.82887195,  0.87522863],
       [ 0.88325189,  0.67976957,  0.56058782,  0.58438597,  0.10571746],
       [ 0.27305838,  0.72306733,  0.01630463,  0.86069002,  0.9458257 ],
       [ 0.23113894,  0.30396521,  0.92840314,  0.39544522,  0.59708927],
       [ 0.71878406,  0.91327744,  0.71407427,  0.65388644,  0.416599  ],
       [ 0.83550209,  0.85024774,  0.96788451,  0.72253464,  0.41661953],
       [ 0.61458993,  0.34527785,  0.20301719,  0.10626226,  0.00773484],
       [ 0.87275531,  0.54878131,  0.24933454,  0.29894835,  0.66966912],
       [ 0.59533278,  0.15037691,  0.37865046,  0.99402371,  0.17325722]])
>>> b[a[:,4] == 0]
array([[ 0.59533278,  0.15037691,  0.37865046,  0.99402371,  0.17325722]])
>>> 

To get a find like result instead of using logical indexing use np.where, which returns a tuple of arrays which serve as indices into each dimension:

>>> indices = np.where(a[:, 4] == 0)
>>> b[indices[0]]
array([[ 0.59533278,  0.15037691,  0.37865046,  0.99402371,  0.17325722]])
>>> 
YXD
  • 31,741
  • 15
  • 75
  • 115
  • You demonstrated something more akin to logical indexing. Matlab's `find` directly returns the index (or indices) that satisfy a condition. For example, `idx = find(a(:,5)==0)` would return `10` for your first array. – horchler Oct 20 '13 at 21:10
  • also agree that this is a duplicate of the question you've linked to – YXD Oct 20 '13 at 21:31