Save data every at loop interation

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

回答(1 个)

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 个评论

i will do them both. In workspace and write it in a file. The problem is that at each loop i want to save the value. If a let the code how it is, the MM is the final resuts and not the result at every loop. I tried to put in a vector the values, at every loop, but i dont be able to do it so well...

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by