Storing for loop using either zeros or ones?

3 views (last 30 days)
I have a 1000 data-sets from a for loop, but it only keeps the last value that was generated. Is there a simple way to store all the data from the for loop into an array. I've tried element multiplication using ones but it still only stores the last data set. Each value is 1x3.
Here is the output, in this case for vECI

Answers (1)

Cris LaPierre
Cris LaPierre on 3 Nov 2022
You overwrite your variable vECI in each loop, leaving you with just the final values. If you want to save the value from every loop, you need to assing the output using indexing.
What is the best way to do it will depend on what you do next with vECI. Compare b and c in the example below
for a = 1:3
b=a;
c(a) = a;
end
b
b = 3
c
c = 1×3
1 2 3

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!

Translated by