Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: boolean comparisons with arrays

Tags:

julia

I have a simple question about boolean comparisons in Julia. How do I translate the following Matlab code to Julia?

Matlab:

% create parameters
o = -3;
mat = [65 -4; 65 -3; 65 -2]

% determine which rows of matrix have column 2 less than o AND column 1 equal to 65.
result = (o < mat(:,2) & mat(:,1) == 65)

I've tried the following in Julia:

# create parameters
o = -3
mat = zeros(3,2)
mat[:,1] = 65
mat[1,2] = -4
mat[2,2] = -3
mat[3,2] = -2
mat

# attempt to create desired result
o .< mat[:,2]                                # this part works
mat[:,1] .== 65                              # this part works
test = (o .< mat[:,2] && mat[:,1] .== 65)    # doesn't work
test = (o .< mat[:,2] .& mat[:,1] .== 65)    # doesn't work
test = (o .< mat[:,2] & mat[:,1] .== 65)     # doesn't work
like image 851
Adam Avatar asked Sep 17 '15 21:09

Adam


1 Answers

It's a matter of operator precedence. & has a higher precedence in Julia than it does in Matlab. Just shift around your parentheses:

test = (o .< mat[:,2]) .& (mat[:,1] .== 65)

See Noteworthy differences from Matlab in the manual for more details (and it's worth reading through the other differences, too).

like image 182
mbauman Avatar answered Sep 30 '22 15:09

mbauman