How to save a vector from each iteration in a loop in a matrix?
30 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Unnamed User
am 27 Feb. 2023
Bearbeitet: Stephen23
am 27 Feb. 2023
Essentially one of the outputs for each iteration of a for loop in my code is a row vector mesauring 1x300. After each iteration of the for loop, for 230 iterations, I'd like to save each row vector so that I end up with a matrix of 230x300, or essentially the 1x300 vector for each iteration value.
output_all(:,i) = output(1,:);
Is the best I've been able to come up with but only saves the final row vector for the loop. Any changes to this throw up errors such as, "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 300-by-1." when I use the following code:
output_all(:,i) = output(:,:);
I've already pre-allocated an empty matrix for the output from this:
output_all = zeros(size(230,300));
And help would be appreciated, I realise it's most likely a simple solution but I cannot seem to figure it out.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 27 Feb. 2023
Bearbeitet: Stephen23
am 27 Feb. 2023
Take a closer look at your preallocation. What does SIZE(..) return?:
size(230,300)
so your preallcoation
zeros(size(230,300))
is exactly equivalent to
zeros(1)
which returns a 1x1 matrix full of ... well, one single zero.
You probably intended this:
zeros(230,300)
Tip: when debugging code, you need to look at what the code is really doing. The code does not care what you think it is doing, your task is to investigate what it is really doing. This means looking at values, classes, sizes, etc.
Weitere Antworten (1)
Torsten
am 27 Feb. 2023
output_all(:,i) = output(1,:).';
instead of
output_all(:,i) = output(1,:);
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!