Conditions else if - efficiency advice

1 view (last 30 days)
Hi,
I want advice on how to make this code more efficient.
We have2 groups S and T. For each I want to attribute the aS or aT variables position that correspond to the respective input (searching in subS and subT, respectively).
Can you help?
Thanks!
input = '003'
% S group
subS = {'001','002','003',...};
aS = {'R','O','O',...};
% T group
subT = {'01','02','03'...};
aT = {'O','O','R',...};
if % S group
if strcmp(input,subS(1))
a = aS(1)
elseif strcmp(input,subS(2))
a = aS(3)
elseif strcmp(input,subS(2))
a = aS(3)
else
...
end
else % T group
if strcmp(input,subT(1))
a = aT(1)
elseif strcmp(input,subT(2))
a = aT(3)
elseif strcmp(input,subT(2))
a = aT(3)
else
...
end
end

Accepted Answer

Stephen23
Stephen23 on 25 Nov 2021
Edited: Stephen23 on 25 Nov 2021
inp = '003';
% S group
subS = {'001','002','003'};
aS = {'R','O','O'};
% T group
subT = {'01','02','03'};
aT = {'O','O','R'};
[X,Y] = ismember(inp,subS);
aS(Y(X))
ans = 1×1 cell array
{'O'}
[X,Y] = ismember(inp,subT);
aT(Y(X))
ans = 0×0 empty cell array

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!