Azzera filtri
Azzera filtri

Conditions else if - efficiency advice

2 visualizzazioni (ultimi 30 giorni)
Nina Perf
Nina Perf il 25 Nov 2021
Modificato: Stephen23 il 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

Risposta accettata

Stephen23
Stephen23 il 25 Nov 2021
Modificato: Stephen23 il 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

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by