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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With