Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading