How to plot multiple graphs on the same figure using a loop?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I have attached the code im working on below. In the when the first for loop executes, the if statement at the bottom executes and prints a graph of how many loops the code has completed against how many cells are on the screen in a new figure. What I want to have is, a larger loop that will print the graph thats plotted in the if statement on the same figure 10 times to show 10 different runs of the code. This would require restarting the code when the first for loop reaches i=100. I cant get any implementation of this to work. Any help would be appreciated.
clear all
close all
clc
%%%Random Seed
size_of_game = 100;
mylife = round(rand(size_of_game,size_of_game));
gen = 0;
noOfLiveCells = [];
for i = 1:100 %here (1)
axis([-size_of_game size_of_game -size_of_game size_of_game])
%Creating the Generation Counter
generation = [];
gen = gen + 1;
l(i) = gen;
generation = [l];
spy(mylife)
grid minor
grid on
%Propagate
newlife = mylife;
for ii = 1:size_of_game
si = ii-1;
ei = ii+1;
veci = si:ei;
veci(veci==0)=size_of_game;
veci(veci==size_of_game+1)=1;
for jj = 1:size_of_game
%%%This is overlap from each side
sj = jj-1;
ej = jj+1;
vecj = sj:ej;
vecj(vecj==0)=size_of_game;
vecj(vecj==size_of_game+1)=1;
num_neighbors = sum(sum(mylife(veci,vecj)))-mylife(ii,jj);
if mylife(ii,jj)
%Any live cell with fewer than two live neighbours dies, as if caused by under-population.
if num_neighbors < 2 %1 - 2, %2 - 3 , %3 - 2
newlife(ii,jj) = 0;
end
%Any live cell with two or three live neighbours lives on to the next generation.
%Any live cell with more than three live neighbours dies, as if by overcrowding.
if num_neighbors > 3 %1 - 3, %2 - 4, %3 - 3
newlife(ii,jj) = 0;
end
else
%Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
if num_neighbors == 3 %1 - 3, %2 - 3, %3 - 2
newlife(ii,jj) = 1;
end
end
end
end
drawnow
mylife = newlife;
livecell = [1];
numberoflives = nnz(conv2(mylife,livecell,'valid')==nnz(livecell));
%Number of live still lifes counter
s(i) = numberoflives;
noOfLiveCells = [s];
if i == 100 %Here (2)
plot(generation,noOfLiveCells)
end
end
0 comentarios
Respuestas (1)
Voss
el 30 de Mzo. de 2022
Maybe something like this (create one figure with both the life grid and the number of live cells over time):
clear all
close all
clc
size_of_game = 100;
n_runs = 10;
n_gens = 100;
noOfLiveCells = NaN(n_runs,n_gens);
figure()
life_axes = subplot(1,2,1);
n_cells_axes = subplot(1,2,2);
hold on
grid minor
grid on
axes(life_axes);
for j = 1:n_runs
%%%Random Seed
mylife = round(rand(size_of_game,size_of_game));
for i = 1:n_gens
spy(mylife);
%Propagate
newlife = mylife;
for ii = 1:size_of_game
si = ii-1;
ei = ii+1;
veci = si:ei;
veci(veci==0)=size_of_game;
veci(veci==size_of_game+1)=1;
for jj = 1:size_of_game
%%%This is overlap from each side
sj = jj-1;
ej = jj+1;
vecj = sj:ej;
vecj(vecj==0)=size_of_game;
vecj(vecj==size_of_game+1)=1;
num_neighbors = sum(sum(mylife(veci,vecj)))-mylife(ii,jj);
if mylife(ii,jj)
%Any live cell with fewer than two live neighbours dies, as if caused by under-population.
if num_neighbors < 2 %1 - 2, %2 - 3 , %3 - 2
newlife(ii,jj) = 0;
end
%Any live cell with two or three live neighbours lives on to the next generation.
%Any live cell with more than three live neighbours dies, as if by overcrowding.
if num_neighbors > 3 %1 - 3, %2 - 4, %3 - 3
newlife(ii,jj) = 0;
end
else
%Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
if num_neighbors == 3 %1 - 3, %2 - 3, %3 - 2
newlife(ii,jj) = 1;
end
end
end
end
drawnow
mylife = newlife;
livecell = 1;
%Number of live still lifes counter
noOfLiveCells(j,i) = nnz(conv2(mylife,livecell,'valid'));
end
% plot after the "generations" loop:
plot(n_cells_axes,1:n_gens,noOfLiveCells(j,:));
end
0 comentarios
Ver también
Categorías
Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!