Help with looping small fireforest simulation

Hi
Im having trouble with looping my fireforest simulation which is currently in matrix form. Updating the fireforest once is possible but I'm not sure how to loop it for example 3 steps or until the cells are all burned out(which is =0).
This is the sequence:
n = 5;
A = ones(n);
A(1:2,1) = 0; A(n,n-1:n) = 0;
A(n,3) = 2; A(n-1,n-1:n) = 2
[m,n] = size(A);
B = A;
for i = 1:n
for j = 1:n
if A(i,j) == 2
B(i,j) = 0;
elseif A(i,j) == 1
antal = antalgrannar(A,i,j,n);
if antal >= 1
if rand(1) < 0.5
B(i,j) = 2;
end
end
end
end
end
disp (B);
And this is the function the sequence depends on:
function antal = antalgrannar(A,i,j,n)
igrannar = max(1,i-1):min(n,i+1);
jgrannar = max(1,j-1):min(n,j+1);
if A(i,j) == 2
antal = sum(sum(A(igrannar, jgrannar)==2))-1;
else
antal = sum(sum(A(igrannar, jgrannar)==2));
end
end
For further explanations since comments arent available in the code. Its a 5x5 matrix where values such as 0, 1 and 2 exist. 0= burned forest, 1=healthy forest and 2= a burningforest. The 5x5 matrix starts out with designated 0 and 2s specifically to start the fire. The function point to that there is a 50 % chance for a healthy forest(1) which is close to a burning forest to burn hence if rand code.
Back to the point, how am i suppose to loop it for n ammount of times or until the whole 5x5 matrix is fully burnt=0?

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 5 May 2019

Community Treasure Hunt

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

Start Hunting!