0

In this page https://courses.cit.cornell.edu/bionb441/CA/forest.m

I found a code named "Forest Fire" I am trying to figure out how this code works for educational purposes.

Here are the rules:

Cells can be in 3 different states. State=0 is empty, state=1 is burning and state=2 is forest.

If one or more of the 4 neighbors of a cell is burning and it is forest (state=2) then the new state is burning (state=1).

A cell which is burning (state=1) becomes empty (state=0).

There is a low probablity (0.000005) of a forest cell (state=2) starting to burn on its own (from lightning).

There is a low probability (say, 0.01) of an empty cell becoming forest to simulate growth.

what it is not very clear how it works is...

sum = (veg(1:n,[n 1:n-1])==1) + (veg(1:n,[2:n 1])==1) + ...
       (veg([n 1:n-1], 1:n)==1) + (veg([2:n 1],1:n)==1) ;

veg = 2*(veg==2) - ((veg==2) & (sum> 0 | (rand(n,n)< Plightning))) + ...
     2*((veg==0) & rand(n,n)< Pgrowth) ;

There is no problem in running the code, it just I am confused what are these vectors (sum and veg). Especially what makes (veg(1:n,[n 1:n-1])==1).

What I see is that, both are matrixes and veg is the data of the plot (matriz with 0's 1's and 2's).

I really appreciate any help you can provide.

gariepy
  • 3,576
  • 6
  • 21
  • 34
arnirx
  • 1
  • 3

2 Answers2

0

Binary comparison operators on a matrix and a scalar return the matrix of elements of that binary comparison with the scalar and the corresponding element of the original matrix.

sum is a matrix in which each cell contains the number of adjacent cells in the corresponding matrix veg that are on fire (==1).

(veg(1:n,[n 1:n-1])==1) is a matrix of logical 1s and 0s (I don't know if the data type is static or dynamic) in which each cell equals 1 when the cell to the left of the corresponding one in veg is on fire (==1).

0

https://courses.cit.cornell.edu/bionb441/CA/

Look at the URL, go back up the tree to see the source.

The rule: Cells can be in 3 different states. State=0 is empty, state=1 is burning and state=2 is forest. If one or more of the 4 neighbors if a cell is burning and it is forest (state=2) then the new state is burning (state=1). There is a low probablity (say 0.000005) of a forest cell (state=2) starting to burn on its own (from lightning). A cell which is burning (state=1) becomes empty (state=0). There is a low probability (say, 0.01) of an empty cell becoming forest to simulate growth. The array is considered to be toroidly connected, so that fire which burns to left side will start fires on the right. The top and bottom are similarly connected. The update code:

sum = (veg(1:n,[n 1:n-1])==1) + (veg(1:n,[2:n 1])==1) + ...
       (veg([n 1:n-1], 1:n)==1) + (veg([2:n 1],1:n)==1) ;

veg = ...
     2*(veg==2) - ((veg==2) & (sum> 0 | (rand(n,n)< Plightning))) + ...
     2*((veg==0) & rand(n,n)< Pgrowth) ;

Note that the toroidal connection is implemented by the ordering of subscripts.