2

I am wondering if there is a python-numpy operator to compare two vectors of the same shape. Specifically, enter image description here

Can I get the results directly through a numpy API and what is it? Thanks very much!

1
  • float or integers? Commented Apr 1, 2020 at 17:39

1 Answer 1

2

If your arrays are a and b:

c = ((np.repeat(a, b.shape[0]).reshape(a.shape[0], b.shape[0]) - b) == 0).astype(int) 

or, as hpaulj and FBruzzesi said:

c = (a[:, None] == b).astype(int) 
Sign up to request clarification or add additional context in comments.

4 Comments

simpler: a[:,None]==b
@hpaulj That was genius!
Just as a final remark, consider converting to integer: (a[:, None] == b).astype(int) to have zeros and ones instead of boolean values.
@BrunoMello It did work. That was amazing with simplicity. Thank you all for your kind and prompt reply.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.