Save data every at loop interation
Show older comments
my code assimilates a value of a signal (matrix A) to a matrix V. Then i want to calcule the medium value of it, save it and then doing again until the i comes to N. somebody could help me?
code:
for i=1:N
V(i,1)=A(i,1)
Z=1/N
MM=sum(V.*Z)
end
Answers (1)
Chad Greene
on 16 Oct 2015
That depends on what you mean by saving. If you just want it in your workspace,
for k=1:N
V(i,1)=A(i,1)
Z=1/N
MM(k)=sum(V.*Z)
end
should do the trick. Notice I changed your i to a k because sometimes Matlab thinks i is the imaginary unit.
If you want to save the data as a .mat file, you can include
save('mydata.mat','Z','MM')
inside the loop, but note that it will overwrite mydata.mat with every iteration of the loop.
If you use the loop I suggested above, preallocate MM by typing
MM = NaN(1,N);
Preallocating before the loop helps speed things up because Matlab just fills in the empty spots instead of resizing the vector with every iteration of the loop.
1 Comment
Gabriel LimaDuarte
on 19 Oct 2015
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!