store result for loop for 2D array

1 Ansicht (letzte 30 Tage)
Grace
Grace am 3 Jun. 2014
Kommentiert: Sara am 3 Jun. 2014
Hi, I have
a=[1 2; 3 4; 5 6];
for m=1:4
if m<=2
b=a([2 3],:)
else
c=a([1 2],:)
end
end
I want to keep the answer for every loop, for example, I want to know the ouptut when m=1, m=2, m=3 and m=4 respectively. How am i going to do this?
Thanks.

Akzeptierte Antwort

Sara
Sara am 3 Jun. 2014
a=[1 2; 3 4; 5 6];
result = cell(4,1);
for m=1:4
if m<=2
result{m}=a([2 3],:)
else
result{m}=a([1 2],:)
end
end
  3 Kommentare
José-Luis
José-Luis am 3 Jun. 2014
Sara
Sara am 3 Jun. 2014
Replace mynum with the index you're interested in:
result{mynum}
e.g., result{3} will give you the result for m=3

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by