Save data every at loop interation
Mostrar comentarios más antiguos
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
Respuestas (1)
Chad Greene
el 16 de Oct. de 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 comentario
Gabriel LimaDuarte
el 19 de Oct. de 2015
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!