Conditions else if - efficiency advice

1 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2021-11-25
编辑:Stephen23 2021-11-25
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by