saving data during iterations

7 次查看(过去 30 天)
I am working on some iterative calculations and I need to be able to save the results of the iteration at various times during the calculation.
I know that I can plot the results of each step with "hold on" and the plot command. What I cannot figure out is how to write the results of each iteration either to the workspace as a new variable or to a file so that I can load it into something else later,
Here is what I have so far
x = [1 2 3 4 5]
y= [1.5, 2.5, 3.5, 4.5, 5.5]
for k=1:10
buff1 = y
for x=1:5
y(x)=buff1(i)+y(i)
end
hold on
plot(x, y)
end
What I want to do is to have a listing of y for each of the 10 iterations.
Thanks
Cliff

采纳的回答

Jesus Sanchez
Jesus Sanchez 2020-2-24
编辑:Jesus Sanchez 2020-2-24
It seems that you are overwriting y in each iteration. Being that the case, I would create a matrix stored_y to save the values of that variable. Something like this. I tested it by setting i = 1.
x = [1 2 3 4 5]
y= [1.5, 2.5, 3.5, 4.5, 5.5]
stored_y = zeros(11,5); % Data for each iteration is stored on rows.
stored_y(1,:) = y; % Saves "first" value of y.
for k=1:10
buff1 = y;
for x=1:5
y(x)=buff1(i)+y(i);
end
stored_y(1+k,:) = y; % Saves calculated value of y
% hold on
% plot(x, y)
end
Now, in order to plot stored_y you could do something like this:
figure
hold on
x=1:5;
for n=1:size(stored_y,1)
plot(x,stored_y(n,:));
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by