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

Respuestas (1)

Chad Greene
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
Gabriel LimaDuarte el 19 de Oct. de 2015
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...

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Oct. de 2015

Comentada:

el 19 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by