0

Assuming I have two matrices, A of dimensions r X c, and B of dimensions r X d (i.e., both matrices have the same number of rows and a different number of columns). Now, I want to compare every column in A to every column in B. That is, I want to find all the pairs (i,j), where column i in matrix A equals column j in matrix B. Obviously, this can be easily done with a loop but I was wondering if there is an efficient fancy numpy style way to do that (where the emphasis is on being efficient)?

Thanks!

2
  • Can you make a reproducible example ? Commented Jun 18, 2021 at 16:33
  • 1
    np.where((A[:,None] == B[...,None]).all(0)) Commented Jun 18, 2021 at 16:49

1 Answer 1

1

This function returns an array with column indices that are equal for both matrices.

def get_equal_columns(m1, m2):
    equal_column_indices = np.where((m1[:, None] == m2[..., None]).all(0))[0]
    return equal_column_indices 
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.