Home > OS >  How to find index in a matrix using another list as a reference using Numpy?
How to find index in a matrix using another list as a reference using Numpy?

Time:11-16

Let A be a matrix:

A = array([[0.        , 0.        , 0.        , ..., 0.        , 0.        ,
    0.        ],
   [0.        , 0.28867513, 0.28867513, ..., 0.        , 0.        ,
    0.        ],
   [0.        , 0.        , 0.        , ..., 0.        , 0.        ,
    0.        ],
   [0.        , 0.        , 0.        , ..., 0.        , 0.        ,
    0.        ],
   [0.        , 0.13363062, 0.13363062, ..., 0.        , 0.        ,
    0.        ]])

B = array([0.70710678, 0.66666667, 0.5       , 0.75      , 1.        ])

I need to find the indexes of B in A.

Expected Output:

Matrix containing position of elements.

I want to perform this using inbuilt numpy commands/ logic and not using list comprehension or for loops.

Update: Already tried using isin, unable to tackle multiple elements with same value in the same row.

Updated with a better example of the problem.

CodePudding user response:

numpy.all has a axis input so you can check if a row/column is all True. To get the index of the row you can use np.where

np.where(np.all(A==B, axis=1))

CodePudding user response:

With your original example

In [436]: A = [[0, 1, 2, 3],
     ...:      [4, 5, 6, 7],
     ...:      [8, 9, 10, 11]]
     ...: 
     ...: B = [2, 5, 11]

A simple list comprehension produces:

In [437]: [a.index(b) for a,b in zip(A,B)]
Out[437]: [2, 1, 3]

index does only gives the first, and raises an error if there isn't a match. We could write a cover function that addresses those issue, catching the error, and repeating itself.

If the inputs are arrays:

In [438]: AA = np.array(A); BB = np.array(B)

We can do a "row-wise" test:

In [439]: BB[:,None]==AA
Out[439]: 
array([[False, False,  True, False],
       [False,  True, False, False],
       [False, False, False,  True]])

and find all the True. Your [2,1,3] is the 2nd array.

In [440]: np.nonzero(_)
Out[440]: (array([0, 1, 2], dtype=int64), array([2, 1, 3], dtype=int64))

again the match isn't so clean with there are variable numbers of matches per row.

If the values are floats, we could use isclose instead:

In [441]: np.isclose(BB[:,None],AA)
Out[441]: 
array([[False, False,  True, False],
       [False,  True, False, False],
       [False, False, False,  True]])

Get all indexes of multiple values in numpy array

seeks multiple matches between two arrays. Both arrays are 1d, but the responses illustrate the complications in working with multiple matches.

  • Related