I have a question regarding the internal code of the findall function. I have an array and I want to find elements that satisfy two conditions. I know that I can use something like findall(f:function,A) where A is the array. I have the following code:
A = [1;2;3;4;5];
findall(x->1<x<4,A)
findall(x->x>1&&x<4,A)
findall(x->x>1&x<4,A)
findall(x->x<4&x>1,A)
The first two commands return the correct answer: 2,3. The third one returns 2,3,4,5 and the fourth one returns 0-element Array.
Why don’t the last two commands work?
The logical and works with or without the brackets (command 2). I was just curious why the bitwise operation doesn’t work as I would imaging the function generates two bits for each element of the array and then computes the and operation, which should result in the correct answer. I found information on the precedence for && and || but not on bit wise operations.
I finally understand it now. It evaluates 1&x as a quantity, since x is nonzero, the value is computed as 1, then the conditions read as x>1<4 which is true. Thus the result is true, when you put them in the bracket it evaluates it as two conditions and gives the right answer.
The same logic applied to my initial question. The third condition reads as: x>1<4 (since all my entries are nonzero), all values other than the first satisfy the condition, thus the answer. The fourth condition reads as x<1>1 which is not true thus it doesn’t return anything.
You can use dump on an expressions to see how the syntax is parsed which can be helpful.
dump(:(x>1&x<4))
Expr
head: Symbol comparison
args: Array{Any}((5,))
1: Symbol x
2: Symbol >
3: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol &
2: Int64 1
3: Symbol x
4: Symbol <
5: Int64 4