1

Given two arrays of arrays A and B, I need to test the equality of each subarray from A (ai) to its corresponding subarray in B (bi):

import numpy as np

a1 = np.array([1, 2, 3])
a2 = np.array([3, 4, 5])
a3 = np.array([2, 4, 6])
A = np.array([a1, a2, a3])

b1 = np.array([3, 2, 1])
b2 = np.array([3, 4, 5])
b3 = np.array([6, 4, 2])
B = np.array([b1, b2, b3])

def compare_arrays(A, B):
    #ret = A == B
    #ret = np.array_equal(A, B)
    return ret

print(compare_arrays(A, B))

Unsurprisingly, the output I get with A == B: [[False True False][ True True True][False True False]].

Unsurprisingly, the output I get with np.array_equal(A, B): False.

The output I would like to get: [[False, True, False]].

I would like to know if there exists an off-the-shelf solution that I have not found or if I should implement my own.

2
  • I guess I could just consider the difference matrix and look for zero rows... Commented Jan 7, 2017 at 15:35
  • Even though you construct A from arrays, the result is a 2d array. That's what you get from A==B. Commented Jan 7, 2017 at 17:07

2 Answers 2

4

You can get logical and results along axis=1 from A == B.

def compare_arrays(A, B):
    ret = np.equal(A, B).all(axis=1)
    return ret
Sign up to request clarification or add additional context in comments.

Comments

-1

You can use something like this:

def compare_arrays(A, B):
    return map(lambda item: not False in item, A==B)

2 Comments

Using map doesn't use the power of numpy.
map loops over the rows of A==B. not False in item is better expressed as all(item). [all(row) for row in A==B] is a clearer version of this approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.