saving data during iterations

조회 수: 13 (최근 30일)
Cliff Shaw
Cliff Shaw 2020년 2월 24일
댓글: Jesus Sanchez 2020년 2월 25일
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
  댓글 수: 2
Cliff Shaw
Cliff Shaw 2020년 2월 25일
Thanks, this does the job perfectly
C
Jesus Sanchez
Jesus Sanchez 2020년 2월 25일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by