Question
MATLAB CODE Clear all variables Set the (square) grid dimensions to 50, and call it n. Make a nXn matrix, called GRID, with a .6
MATLAB CODE
Clear all variables
Set the (square) grid dimensions to 50, and call it n.
Make a nXn matrix, called GRID, with a .6 probability of any given cell having a 1 (active) in it; otherwise, 0 (inactive).
This will make 60% of the cells in the matrix into 1s and 40% into 0s.
Make a vector that goes from 2 to n and then ends with a 1, call it up.
Make a vector that starts with n and then goes from 1 to n-1, call it down. (these vectors will be used as indices for GRID, and they allow screen wrap-around)
Make the colormap gray, with a range of 2.
Start a for loop where i goes from 1 to 1000
Now make a matrix called neighbors that will sum up the number of active neighbors (out of a possible 8) that each cell has.
You do this by adding 8 versions of GRID together, where you use your up and down vectors as the indices.
For example:
GRID(up,:) checks for neighbors where the row index has been increased by 1 but the column index has not been changed. GRID(:,down) checks for neighbors where the row index has not been changed but the column index has been decreased by 1. GRID(up,up) checks for neighbors where both the row and column indices have been increased by 1.
Add up all 8 of these versions of GRID into your neighbors matrix
Now, rather then doing a bunch of if-then clauses to find whether the right number of neighbors of a cell are on or off, you can simply update GRID via a logic (True/False) based updating equation.
For example, when you write neighbors==3, rather then changing that variable to nowequal the number three (as would happen if there were only one equals sign), it returns a matrix with the same dimensions as neighbors in which there are 1s (True) where the logical function was satisfied (because the cell had a 3 in it), and 0s (False) where the logical function was not satisfied (because the cell did not have 3 in it).
So, have the new state of GRID = the truth value of the (inclusive) logical disjunction of neighbors equaling three OR (both GRID already true AND neighbors equaling 2).
Now make an image of GRID (multiplied by 2 to accommodate the grayscale range)
And add a pause of a couple hundred milliseconds for each update.
End your for loop
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started