# How does MATLAB handle vector logic?

5 views (last 30 days)
Tyler Warner on 11 Jul 2018
Edited: James Tursa on 13 Jul 2018
If I have large vectors to consider and a scalar value of interest, such as:
vec1 = randi([0 1], 1, 1000000) % zeros and ones
vec2 = randi([1 1000000],1,1000000) % random integers
val = 50; % value I want to test for
Would this
vec1 & vec2 == val % conditional statement 1
be faster than this?
vec2 == val & vec1 % conditional statement 2
Reason: I learned in C class that in an AND operation, if there exists a single condition that is false, no further testing is done and the output is immediately placed as false. What I am wondering is if MATLAB will do the same in vector notation for each element in the vectors being compared (e.g. conditional statement 1 will have less logical operations than conditional statement 2)?
=====================================
EDIT: I made the following script, thinking that I was correct in my inference.
profile on
vec1 = randi([0 1], 1, 1000000); % zeros and ones
vec2 = randi([1 1000000],1,1000000); % random integers
val = 50; % value I want to test for
for k = 1:1000
tic
vec1 & vec2 == val; % conditional statement 1
toc
tic
vec2 == val & vec1; % conditional statement 2
toc
end
profsave
profile off
Sadly, the results are the opposite of my inference. Conditional statement 1 runs in 2.734s while conditional statement 2 runs in only 1.813s. Why is this the case?

James Tursa on 11 Jul 2018
Edited: James Tursa on 11 Jul 2018
The doc on "if expression" only states that MATLAB "... evaluates an expression ..." and makes no mention of any behind the scenes optimizations. So if there were any hard-coded short circuit logic for certain vector expressions that the parser understands, it certainly isn't documented so you can't count on it. I have never tried any tests to see if such optimizations exist.
If, on the other hand, you are asking how MATLAB evaluates each element of the vector expression and whether the & operator short circuits in this case the answer is probably no because MATLAB has to consider such possibilities as NaN values appearing in the expression. So even if the first operand is 0, it has to check the result of the second operand to see if there is a NaN before it can generate a result. The "&&" and "||" operators do short circuit, however, and this is documented. E.g.,
>> 0 & NaN
??? Error using ==> and
NaN's cannot be converted to logicals.
>> 0 && NaN
ans =
0
>> 1 | NaN
??? Error using ==> or
NaN's cannot be converted to logicals.
>> 1 || NaN
ans =
1
##### 2 CommentsShowHide 1 older comment
James Tursa on 13 Jul 2018
Edited: James Tursa on 13 Jul 2018
Sadly, no. I can only venture to guess that the parser recognizes the syntax of the 2nd case and happens to have optimized (multi-threaded? single loop in background?) code for it that gets called, whereas it doesn't recognize the 1st syntax as something it has optimized code for so it calls some more generic code (e.g., turns vec1 into a full logical vector first). E.g., if you do this it will throw off the timing of the "fast" syntax even though the order of the operands is the same:
vec2 == val & logical(vec1);
or
vec2 == val & vec1 ~= 0;
Which is why I am guessing it is a syntax recognition thing.
Background optimizations such as these (if that is in fact what is going on) can easily change from release to release, so you can't necessarily expect that your code will perform similarly in a different version of MATLAB.
For comparison, a simple C-mex routine to do this calculation runs in less than half the time of the "fast" m-code syntax on my machine.

### Categories

Find more on Logical in Help Center and File Exchange

R2017a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!