Avoid evaluation in vectorised conditional expressions

4 views (last 30 days)
Christian B on 31 Jan 2023
Commented: Walter Roberson on 31 Jan 2023
Dear community,
It is well known how to vectorise for-loops that contain if-conditions, see, e.g. https://de.mathworks.com/matlabcentral/answers/17737-how-can-i-vectorize-function-with-if-statement, resulting in functions like (taken from above link):
function result = myfunc(x);
result = zeros(size(x));
idx1 = x>3;
idx2 = (x.^2 + x) > 9;
result(idx1) = 1;
result(idx2) = 2;
result(~(idx1|idx2)) = 3;
This principle works very well. However, in my case, result(idx1) and result(idx2) would be quite lengthy and expensive expressions. The problem is that both expressions are always evaluated for the entire input x, no matter if idx1 or idx2 are true or not. My question is if there is a way to avoid the evaluation of, e.g., the right hand side of result(idx2) if idx2 is false. Just as it would be in an if-elseif-construct.
I'm aware of alternative constructs with arrayfun or a "classical" for-loop with nested if-elseif-construct, but these workarounds are not efficient.
Any help is much appreciated, thank you!

Torsten on 31 Jan 2023
Moved: Torsten on 31 Jan 2023
If your right-hand side contains expressions with x, you must replace x by x(idx1) resp. x(idx2).
2 CommentsShowHide 1 older comment
Walter Roberson on 31 Jan 2023
For future reference:
There are situations in which the cost of extracting a subset of data, computing with it, and expanding the results back into the appropriate locations, can exceed the savings of not performing the operation on the entire matrix.
sin() is implemented using hardware instructions. Those are potentially slower than a conditional memory move, but since the conditional move is going to need to be a loop at some level (you cannot know the relative offset without having evaluated all previous tests), sometimes it can be faster to just do the instruction and ignore or overwrite the results later. sin applied over a block of consecutive locations and be unrolled into several SIN hardware instructions in a row.