Filter löschen
Filter löschen

Conditions else if - efficiency advice

2 Ansichten (letzte 30 Tage)
Nina Perf
Nina Perf am 25 Nov. 2021
Bearbeitet: Stephen23 am 25 Nov. 2021
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

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Nov. 2021
Bearbeitet: Stephen23 am 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

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by