Plotting a variable as a function of the iteration number in a for loop

Hi,
Basically I have loop, within which is a 12X12 array of zeros and ones. For each cycle of the loop I need to sum the value of the array(which I am able to do) and then plot this value against the iteration/loop number.
Obviously the value of the array changes for each loop so I am a bit stuck as to how to plot the graph.
Eg. Do I output the values for each loop to a seperate new array/file, which I then draw upon at the end of the loop to create the graph? Or is there some way to construct a graph in stages as the program loops-essentially holding the plot figure and adding new points to the existing plot for each iteration.
Any help would be very much appreciated! If I have not explained very well just let me know and I can try and explain further/provide code snippets.
Thanks in advance,
Mike Scott

 Respuesta aceptada

Here is an example. Note that if your loop index is 1-to-N, then you won't need the loop counter and this can be simplified in obvious ways. I do have to wonder, however, if you can avoid the loop altogether. Only a look at your code can shed light here...
lp_idx = 10:.5:30; % This is the loop index.
S = zeros(1,length(lp_idx)); % Pre-allocation.
cnt = 1; % Loop counter.
for ii = lp_idx
t = magic(ii); % An array which changes in loop
S(cnt) = sum(t(:)); % The sum of each array
cnt = cnt + 1; % Increment the counter.
end
plot(lp_idx,S) % Plot sum vs. loop index
xlabel('Loop Index')
ylabel('Matrix Sum')

5 comentarios

%
% This is where I should type my 'help facily'. Everything types here
% before the executable command lines will be displayed when the user types
% help gameoflife into the command window.
clear all
clc
A = [0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 1 1 1 1 1 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 1 1 1 1 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 1 1 1 1 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0];
B = [0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 1 1 1 1 1 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 1 0 0 0 0 0 0;0 0 0 0 1 0 0 0 0 0 0 0;0 0 0 1 0 0 0 0 0 0 0 0;0 0 1 0 0 0 0 0 0 0 0 0;0 1 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0 0 0];
mycolmap=[1 1 1;0.5 0.5 0.5]; %Define color map
colormap(mycolmap)%set matlab's colour matrix equal to mycolmap
done = 0;
while (done ~= 1)
start = input('Enter the desired starting condition, 1 or 2: ');
if(start == 1)
disp(['You have selected choice ' num2str(start)])
done = 1;
image(0.5, 0.5, A(2:11, 2:11)+1)%display starting condition A(+1 is because...)
C = A;
elseif(start == 2)
disp(['You have selected choice ' num2str(start)])
done = 1;
image(0.5, 0.5, B(2:11, 2:11)+1)%display starting condition B(+1 is because...)
C = B;
else
disp('Starting condition not recognised. Please re-select');
end
end
T=zeros(12,12);% create a 12X12 grid of zeros, for next iteration
title('Game of life')
grid on
% axis([0 10 0 10])
'linewidth';2;
pause(1)
iter = input('Enter the number of iterations you wish to observe: '); %This allows the user to select the number of iterations they wish the game of life program to carry out
%iter=1,pause,iteration;
fps = input('Enter the delay between each frame(in seconds) you would like to view the generation in: ');
for n=1:iter %This carries out the game of life rules a set number of times as decided by the user.
for i=2:11;
for j=2:11;
C(i,j); %selects each element of the matrix individually
G=C(i-1:i+1,j-1:j+1);%creates a 3X3 matrix around each element of the original matrix
S(i,j)=sum(sum(G))-C(i,j); %Sums the elements of the 3X3 grid minus the centre element value to give the number of neighbours
%disp('Number of alive cells: ')
% This next section applies the Game of Life rules, using the summed
% values for neighbours for each element in the original matrix.
if (C(i,j)==1) && (S(i,j)==2)||(S(i,j)==3);
T(i,j)=1;
elseif (C(i,j)==0 && S(i,j)==3);
T(i,j)=1;
elseif (C(i,j)==1 && (S(i,j)==0)||(S(i,j)==1));
T(i,j)=0;
else (C(i,j)==1 && (S(i,j)>=4));
T(i,j)=0;
end
end
end
image(0.5, 0.5,T(2:11, 2:11)+1);%displays next iteration of life on a new matrix
title(['Game of life. Generation number: ' num2str(n)]);
grid on
axis([0 10 0 10])
alive=sum(sum(T));
disp(['Generation number: ' num2str(n)]); %displays the corresponding generation number as the figure of the generation evolves
disp(['Number of alive cells: ' num2str(alive)]);
C=T;
%K(i,j)=sum(sum(T));
pause(fps)%delay between each frame(iteration), as defined by user
end
figure(2);hold; % This brings up a new figure(the active figure)or uses figure 2, which the plot will be stored in and hence will not overwrite the figure displaying the game of life. Hold-opens the figure for multiple plots
plot(n,alive);
I thought I'd best just put the whole program. I have had to create a game of life program that visualises the generation of cells using some rules. The part I am concerned with now is the plotting of the number of alive cells (given by alive=T..) against the loop/iteration number.
You can see the value for T is generated within each loop but the actually plot command is outside the loop.
Hope this makes some kind of sense to you?
Thanks in advance,
Mike
The last section of the code is the relevant part, the rest is just if you needed a greater understanding of the program itself.
Mike
So just before your loop, put this:
alive = zeros(1,iter);
Then in the loop:
alive(n)=sum(T(:));
and to plot:
plot(1:n,alive);
Note that sum(T(:)) is preferred over sum(sum(T)).
Wow I have been reading through so many topics without any luck. That's great, thanks so much for your response Matt.
Much appreciated,
Mike

Iniciar sesión para comentar.

Más respuestas (1)

vega valentine
vega valentine el 11 de Abr. de 2011
don't you just have to plot it outside the loop? I think what you need to do is just store every values you needed in every loop into a separate array/variable, and then after the looping is done, you can plot that array/variable.

2 comentarios

Yes, the plot command is written outside of the loop. You have cleared up my query regarding whether to store the values as the program loops into a seperate array, which makes sense now. Is there a command that I should use to set up this array and then assign the values generated in each loop to?
Thanks for your reply
Post the code you're using, formatting with the code button.

Iniciar sesión para comentar.

Categorías

Más información sobre Conway's Game of Life en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 11 de Abr. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by