concatenate
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
God'sSon
am 28 Mär. 2011
Bearbeitet: Rajeev
am 26 Mai 2014
I have a for-loop that executes commands and displays the answer for each iteration. Please, can someone tell me how to concatenate all the answers into an array instead of having the results displayed independently for each iteration. cheers.
2 Kommentare
Akzeptierte Antwort
Walter Roberson
am 28 Mär. 2011
Paulo answered the question as you phrased it, but if you know the exact number of results you are producing (or if the maximum number of results is "reasonably close" to the likely number of results), then you are better off pre-allocating memory for the results:
a = zeros(10,1);
for b = 1:10
a(b) = 3*b+5; %for example
end
When you get to thousands of elements, pre-allocating can be much much faster.
2 Kommentare
Walter Roberson
am 28 Mär. 2011
b = zeros(size(a));
for i = 1:size(a,1)
b(i,:) = sort(a(i,:))
end
Or more simply,
b = sort(a,2);
Weitere Antworten (1)
Paulo Silva
am 28 Mär. 2011
a=[]
for b=1:10
a=[a b] %replace the b with the data you want to store per iteration
end
a %result is inside this variable
The code doesn't do any memory pre-allocation!!
0 Kommentare
Siehe auch
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!