complicated for loop with 2 requirements or constraints
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Grace
am 4 Jun. 2014
Kommentiert: Grace
am 4 Jun. 2014
Hi,I have
a=[1 2; 3 4; 5 6;7 8];
Suppose I want my result to have two sets of number, which set 1 is [1 2; 3 4; 5 6] and set 2=[3 4; 5 6;7 8].
result=cell(2,1);
for m=1:2
for i=0:1
k=1:3;
result{m}=a(k+i,:);
end
end
This output shows 2 similar set of numbers. What can I do? Do I make myself clear?
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Roger Wohlwend
am 4 Jun. 2014
In the second loop you first (when i = 0) save a matrix in result{m} and then you override it when (i = 1). So the inner loop has no effect.
I do not understand why you use loops at all. You could simply write:
result{1} = a(1:3,:);
result{2} = a(2:4,:);
Or if you want a loop:
for m = 1 : 2
result{m} = a(m:m+2,:);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices 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!