Updated.
I have this code structure inside a loop. x, y and z are 1/0 variables. In this case the order of the elseif statement is: first x is evaluated, then y then z
if x
%code for case x
elseif y
%code for case y
elseif z
%code for case z
end
I would like to define a varible order (for the previous case order={'caseX','caseY','caseZ'}), where I can change the order in which the elseif statement is evaluated. So if I set order={'caseZ','caseX','caseY'}, I should get this: (first z is evaluated, then x then y)
order={'caseZ','caseX','caseY'}
%%% then
if z
%code for case z
elseif x
%code for case x
elseif y
%code for case y
end
An hint will be appreciated.

2 Comments

Ive J
Ive J on 17 Dec 2020
Edited: Ive J on 17 Dec 2020
I'm not sure if I understood you correctly, but seems easier if you use switch/case. See here:
KSSV
KSSV on 17 Dec 2020
Have a look on switch.

Sign in to comment.

 Accepted Answer

James Tursa
James Tursa on 17 Dec 2020
Edited: James Tursa on 17 Dec 2020
Does this do what you want:
order = [1 2 3]; % or whatever
for k=1:numel(order)
switch order(k)
case 1
if( x )
% code for case x
break;
end
case 2
if( y )
% code for case y
break;
end
case 3
if( z )
% code for case z
break;
end
end
Or if you prefer the case selection could be for characters 'x', 'y', and 'z' instead of numbers 1, 2, 3.
The key is to have those break statements inside the cases so that the if-elseif-elseif control structure is emulated properly.

9 Comments

Rub Ron
Rub Ron on 17 Dec 2020
so, the if-elseif-elseif is replaced by a for-switch-if . Any idea if this replacement is computationaly expensive? Those lines are executed hundred or thousands of times.
James Tursa
James Tursa on 17 Dec 2020
Edited: James Tursa on 17 Dec 2020
Probably the only way to see the difference is to code it both ways (i.e., a hard-coded order vs the above logic) and use the profiler.
Rub Ron
Rub Ron on 17 Dec 2020
Edited: Rub Ron on 17 Dec 2020
At the end I combine your answer with Bruno's:
b = [x y z]; % logical conditions (in if/elseif)
[~,i] = ismember(prefered_order, {'x','y','z'});
j = find(b(i), 1, 'first');
switch prefered_order(j)
case 'x'
% code for case x
case 'y'
% code for case y
case 'z'
% code for case z
end
How can this work for you? I don't see any looping that will emulate the if-elseif-elseif control structure.
in this case a loop is not needed. The key is in:
j = find(b(i), 1, 'first');
consider:
prefered_order = {'z','x','y'}
Ah, yes, I missed that ...
I like the combo solution.
Rub Ron
Rub Ron on 17 Dec 2020
Thanks Bruno, I wish I can accept both answers at the same time, with only one click
I gave Bruno a vote since he contributed a key part of the solution.

Sign in to comment.

More Answers (2)

prefered_order = 'xzy';
b = [x y z]; % logical conditions (in if/elseif)
code_to_be_exec = { @xcode, @ycode, @zcode }; % code in the if elseif block put in functions
[~,i] = ismember(prefered_order, 'xyz');
j = find(b(i), 1, 'first');
if ~isempty(j)
feval(code_to_be_exec{i(j)});
end

2 Comments

Rub Ron
Rub Ron on 17 Dec 2020
do you think there is a way to preseve the if elseif structure? for easy-to-read purpose.
Bruno Luong
Bruno Luong on 17 Dec 2020
Edited: Bruno Luong on 17 Dec 2020
Of course, unroll all 6 possible permutations
if strcmp(prefered_order, 'xyz')
% your original if/else
elseif strcmp(prefered_order, 'yxz')
% another order...
elseif strcmp(prefered_order, 'zyx')
....
end

Sign in to comment.

KSSV
KSSV on 17 Dec 2020
Are you looking something like this.
if z
%code for case z
order = ['case','z'] ;
elseif x
%code for case x
order = ['case','x'] ;
elseif y
%code for case y
order = ['case','y'] ;
end

3 Comments

Rub Ron
Rub Ron on 17 Dec 2020
Hi, no. What I want is to chance the order of the if else if statement, base on a variable order that indicates which order should be considered. Is it clear what I am trying to achieved?
order = 'z'
if strcmpi(order,'z')
%code for case z
order = ['case','z'] ;
elseif strcmpi(order,'x')
%code for case x
order = ['case','x'] ;
elseif strcmpi(order,'y')
%code for case y
order = ['case','y'] ;
end
Also have a look on switch.
Rub Ron
Rub Ron on 17 Dec 2020
I updated my post, hopefully now it is clearer.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 17 Dec 2020

Commented:

on 17 Dec 2020

Community Treasure Hunt

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

Start Hunting!