How to plot multiple graphs on the same figure using a loop?
16 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Voss
2022 年 3 月 30 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Conway's Game of Life についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!