Storing for loop using either zeros or ones?

2 vues (au cours des 30 derniers jours)
Rowan McLoughlin
Rowan McLoughlin le 3 Nov 2022
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

Réponses (1)

Cris LaPierre
Cris LaPierre le 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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by