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 个)
Chad Greene
2015-10-16
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.
类别
在 帮助中心 和 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!