Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - array of boolean element-wise operation

Tags:

julia

I do not understand the following element wise boolean operation result (see arrow on last part):

A = [1,2,3,1,2]
B = [1,2,1,3,2]

julia> A.==1
5-element BitArray{1}:
  true
 false
 false
  true
 false

julia> B.==1
5-element BitArray{1}:
  true
 false
  true
 false
 false

julia> A.==1 .& B.==1
5-element BitArray{1}:
  true
 false
 false
  true        <----- I expect this to be false
 false

The 4th element of A.==1 .& B.==1 should be false because it is (true & false). Can someone explain?

like image 778
Timothée HENRY Avatar asked Jun 28 '19 02:06

Timothée HENRY


1 Answers

Operator precedence issue.

You have (by omission):

(A .== (1 .& B) .==1)

You need:

(A .== 1) .& (B .==1)

Relevant docs. Note that & (defined as multiplication) comes before == (defined as comparison).

like image 135
Colin T Bowers Avatar answered Oct 24 '22 01:10

Colin T Bowers