Conditions else if - efficiency advice

1 回表示 (過去 30 日間)
Nina Perf
Nina Perf 2021 年 11 月 25 日
編集済み: Stephen23 2021 年 11 月 25 日
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 Center および File ExchangeCell Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by