1

I have huge numpy matrix. Let us say

A['a1'] = [1,2,3,6]
A['a3']= [3,4,3,7]
A['a4']= [4,6,8,7]
B['b2'] = [2,2,2,4]

A['a1']     A['a3']  A['a4']  B['b2']
1              3         4     2
2              4         6     2
3              3         8     2  
6              7         7     4

I want to select the index where B['b2'] has value 2 and A['a3'] has value 3. So that means I need the index 0 and 2.

For single array I can use np.where, but how can I correlate between that between different arrays. I have been using Pandas before and it was quite easy, but unable to find something to achieve it using numpy.

1 Answer 1

1

You could use a set object {...} combined with its method intersection

import numpy as np

A, B = {}, {} # Optional : to avoid bug in this chunk of code

A['a1'] = [1,2,3,6]
A['a3']= [3,4,3,7]
A['a4']= [4,6,8,7]
B['b2'] = [2,2,2,4]

n1 = np.where(np.array(A['a3'])==3) # {0, 2}
n2 = np.where(np.array(B['b2'])==2) # {0, 1, 2}

print(set(n1[0]).intersection(n2[0]))
# {0, 2}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. thats nice. why didn't I think of it before. Thanks once again

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.