Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do element-wise comparison of two arrays?

I have two arrays:

a = [1,2,3]
b = [1,4,3]

Is there an element-wise comparison method in Ruby such that I could do something like this:

a == b

returns:

[1,0,1] or something like [TRUE,FALSE,TRUE].

like image 491
Michael Avatar asked Jan 19 '12 07:01

Michael


People also ask

How do you compare if 2 arrays are equal to each other?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

How do you do element-wise comparison in Python?

To perform element-wise comparison of two string arrays using a comparison operator, use the numpy. compare_chararrays() method in Python Numpy. The arr1 and arr2 are the two input string arrays of the same shape to be compared.

How do you compare two arrays in Python?

Compare Two Arrays in Python Using the numpy. array_equiv() Method. The numpy. array_equiv(a1, a2) method takes array a1 and a2 as input and returns True if both arrays' shape and elements are the same; otherwise, returns False .


1 Answers

Here's one way that I can think of.

a = [1, 2, 3]
b = [1, 4, 3]

a.zip(b).map { |x, y| x == y } # [true, false, true]
like image 86
Anurag Avatar answered Oct 03 '22 23:10

Anurag