Compare if the elements of two vectors are equal in Julia

I am trying to get the same behavior as R's == when applied to two vectors that get the comparison for each element in the vector.

a <-  c(1,2 ,3 )
b <-  c(1, 2 ,5 )
a==b
#[1]  TRUE  TRUE FALSE

I Julia, I came up with a very clumsy way of doing it, but now I wonder if there are easiest ways out there.

a = [1 2 3 ]
b = [1 2 5 ]
a == b  #this does not return what I want.
#false  

rows_a =size(a)[2]
equal_terms =ones(rows_a)
for i in  1:rows_a 
        equal_terms[i] =(a[i] == b[i])
end
equal_terms
#1.0
#1.0
#0.0

Thank you in advance.

Upvotes: 3

Views: 1461

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

In Julia you need to vectorize your operation:

julia> a .== b
1×3 BitMatrix:
 1  1  0

Julia contrary to Python and R will require explicit vectorization each time you need it. Any operator or function call can be vectorized just by adding a dot ..

Please note that a and b are horizontal vectors and in Julia such are presented as 1×n matrices. Vectors in Julia are always vertical.

Upvotes: 7

Related Questions