Storing data from a loop

2 vues (au cours des 30 derniers jours)
William Harris
William Harris le 21 Mai 2020
Modifié(e) : per isakson le 21 Mai 2020
I am struggling to store data from a loop, i need the data so that i am able to plot it. Thanks in advance!
for i=2:50
i=find((r>=t2n)&(r<t2p));
Y1=y(i);
Y2=mean(Y1)
end

Réponses (1)

per isakson
per isakson le 21 Mai 2020
Modifié(e) : per isakson le 21 Mai 2020
  1. Pre-allocate Y1 and Y2
  2. Assign to elements of Y1 and Y2
  3. Do not change the loop counter, ii, in the loop
The documentation says: Avoid assigning a value to the index variable within the loop statements. The for statement overrides any changes made to index within the loop.
Y1 = nan(1,50);
Y2 = nan(1,50);
for ii=2:50
ii=find((r>=t2n)&(r<t2p));
Y1(ii)=y(ii);
Y2(ii)=mean(Y1(ii))
end

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by