Filter löschen
Filter löschen

How do I break a complex loop with a multiple-if structure?

2 Ansichten (letzte 30 Tage)
GEORGIOS BEKAS
GEORGIOS BEKAS am 18 Okt. 2017
Bearbeitet: Matt J am 18 Okt. 2017
I am writing a code that has the following form:
for i = 1:size(interiorcolumns,1)
if interiorcolumns{i}(6) == heightsmatrix(1)
condition 1
elseif interiorcolumns{i}(6) == heightsmatrix(2)
condition 2
elseif interiorcolumns{i}(6) == heightsmatrix(3)
condition 3
elseif interiorcolumns{i}(6) == heightsmatrix(4)
condition 4
elseif interiorcolumns{i}(6) == heightsmatrix(5)
condition 5
else
condition 6
end
end
When the heightsmatrix, is smaller than six row elements, I want the redundant if statements not to be taken into account. How can I do that?

Akzeptierte Antwort

Matt J
Matt J am 18 Okt. 2017
Bearbeitet: Matt J am 18 Okt. 2017
Another way
for i = 1:size(interiorcolumns,1)
switch find( interiorcolumns{i}(6) == heightsmatrix, 1 )
case 1
condition 1
case 2
condition 2
case 3
condition 3
case 4
condition 4
case 5
condition 5
otherwise
condition 6
end
end

Weitere Antworten (1)

Matt J
Matt J am 18 Okt. 2017
Bearbeitet: Matt J am 18 Okt. 2017
I would just fill the missing elements with NaNs
heightsmatrix(end+1:6)=nan;

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by