Question

% Create a matrix which has 150 rows and 200 columns % Use the rand function...

% Create a matrix which has 150 rows and 200 columns
% Use the rand function to generate that matrix with radom values of entries
% are between 0 and 1
a = rand(150,200);
b = zeros(1,300);
for i = 1:150
    for j = 1:200
        if rand() < 0.9 % 90% of rand() value less than 0.9
            a(i,j) = 0; % Assign 0 for dead cells
        else a(i,j) = 1; % Assign 1 for living cells
        end
    end
end

for k = 1:300
    for j = 1:200
        for i = 1:150
            south = i + 1;
            if south > 150
                south = 1;
            end
         north = i - 1;
         if north == 0
             north = 150;
         end
    
        east = j + 1;
        if east > 200
            east = 1;
        end
        west = j -1;
        if west == 0
            west = 200;
        end

    neighbors = a(north,j) + a(north,east) + a(i,east) + a(south,east) + a(south,j)+a(south,west)+a(i,west)+a(north,west);
%Update new generations for the cells
     if a(i,j) == 1 && (neighbors == 2 || neighbors == 3)
         a(i,j) = 1;
     elseif a(i,j) == 1 && (neighbors < 2 || neighbors > 3)
         a(i,j) = 0;
     elseif a(i,j) == 0 && neighbors == 3
         a(i,j) = 1;
     end
        end
    end
    b(k) = sum(sum(a));
    imagesc(a);
     drawnow
end
    %Plot the number of living cells vs time
       time = linspace(0,300,300);
       figure
       plot(time,b)

Could anyone help me to check this script? I created a matrix "a" with the size of 150 by 200, in the for loop, I want to update the sum of all entries of the matrix after each stepsize of k and store it to the b vector. However, I feel like I might have done something wrong because when I check the vector b in the worksapce, b(1); b(2).... is so small. Thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

we can update this script in maths work lab

% Create a matrix which has 150 rows and 200 columns
% Use the rand function to generate that matrix with radom values of entries
% are between 0 and 1
a = rand(150,200);
b = zeros(1,300);
for i = 1:150
    for j = 1:200
        if rand() < 0.9 % 90% of rand() value less than 0.9
            a(i,j) = 0; % Assign 0 for dead cells
        else a(i,j) = 1; % Assign 1 for living cells
        end
    end
end

for k = 1:300
    for j = 1:200
        for i = 1:150
            south = i + 1;
            if south > 150
                south = 1;
            end
         north = i - 1;
         if north == 0
             north = 150;
         end
    
        east = j + 1;
        if east > 200
            east = 1;
        end
        west = j -1;
        if west == 0
            west = 200;
        end

    neighbors = a(north,j) + a(north,east) + a(i,east) + a(south,east) + a(south,j)+a(south,west)+a(i,west)+a(north,west);
%Update new generations for the cells
     if a(i,j) == 1 && (neighbors == 2 || neighbors == 3)
         a(i,j) = 1;
     elseif a(i,j) == 1 && (neighbors < 2 || neighbors > 3)
         a(i,j) = 0;
     elseif a(i,j) == 0 && neighbors == 3
         a(i,j) = 1;
     end
        end
    end
    b(k) = sum(sum(a));
    imagesc(a);
     drawnow
end
    %Plot the number of living cells vs time
       time = linspace(0,300,300);
       figure
       plot(time,b)

Add a comment
Know the answer?
Add Answer to:
% Create a matrix which has 150 rows and 200 columns % Use the rand function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • Question 23 Complete the programs by adding the lines or parts of lines that have been...

    Question 23 Complete the programs by adding the lines or parts of lines that have been omitted, or show output. <<<<<<<<<<<<<<<<<<<<<<<<    >>>>>>>>>>>>>>>>>>>>>>>>> Corporate Sales Data Output #include <iostream> #include <fstream> using namespace std; // Constant for array size const int SIZE = 12; // Declaration of the Division structure struct Division { Blank 1 // Division name A. Add code in this box Blank 2 // Quarter number B. Add code in this box Blank 3 // Quarterly sales...

  • Suppose the hurricane in Figure 2 has stalled off the southeast coast of the United States....

    Suppose the hurricane in Figure 2 has stalled off the southeast coast of the United States. Further suppose that the peak winds around the storm are 115 mi/hr and that its central pressure is 934 millibars (27.58 inches). If the hurricane begins to move due west at 10 mi/hr, answer the following questions assuming that the hurricane experiences no change in its intensity. a. On which side of the hurricane (north, south, east or west) would you expect the winds...

  • I am creating a MATLAB game for my school project. The goal of the game is...

    I am creating a MATLAB game for my school project. The goal of the game is to create a 'Treasure Hunt Game' that asks the user to input the number of players, the difficult (easy, medium, or hard), and asks the user(s) to pick spots on a matrix until the correct spot is chosen, therefore winning the game. If a player misses the spot, the command window doesn't show how far away the treasure is, but what direction it is...

  • hi which code should I use to get the following matrix and be able to change...

    hi which code should I use to get the following matrix and be able to change the number of rows. the code in the picture doesn't work for me for some reason. thank you. I use matlab program it needs to look like this and be a 10 by 10 matrix. 40 SC Matrix C sym (zeros) C(1,1) = 1; tsets the element 1,1 to be egy 42 - 44 - 45 - 46 - for n = 2:N starts...

  • In this graded tutorial you will learn how to use Excel’s INDEX and MATCH functions. If you are familiar with the VLOOKUP function, INDEX/MATCH is often seen as a better method to accomplish the same...

    In this graded tutorial you will learn how to use Excel’s INDEX and MATCH functions. If you are familiar with the VLOOKUP function, INDEX/MATCH is often seen as a better method to accomplish the same goal. The Excel INDEX function returns a value in a range based on the row and/or column numbers that are specified. Its format is INDEX(array, row_num, [column_num]). Note that column_num is optional. For example, assume you have the simple data below: A B 1 North...

  • The question is attached in following two photos. Please use Matlab if you exactly know how to do it. Thank you. Linorm.m Create a function Linorm which takes one argument, M a square matrix and...

    The question is attached in following two photos. Please use Matlab if you exactly know how to do it. Thank you. Linorm.m Create a function Linorm which takes one argument, M a square matrix and computes the LI-norm of the matrix. This is a number associated to each square matrix M, denoted lIMll, as follows. For each column of the matrix we add together the absolute values of the entries in that column, and we then take the maximum of...

  • can someone please help me with 2-5? I want to make sure I have them done...

    can someone please help me with 2-5? I want to make sure I have them done correctly. I have to do well in this class. thank you nitude and direction of B 2. Vector A has a magnitude of 8.00 units and makes an angle of 45.0° with the positive x-axis. Vector B also has a magnitude of 8.00 units and is directed along the negative xaxis. Using graphical methods, find (a) the vector sum A B and (b) the...

  • using this array I need to create a user created function in Matlab that does the...

    using this array I need to create a user created function in Matlab that does the following I have this so far but I'm so lost could you please make this with comments on what the codes do Lure dueren suuj (COM 94 97 95 19 91 88 95 87 90 72 78 60 98 87 81 79 76 69 192 80 77 73 78 70 80 89 81 186 94 901 b. Find the lowest grade in Math subject...

  • In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the ...

    In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the way that is chosen by MATLAB, which is different from the textbook presentation. An mxn matrix A can be presented as a product of a unitary (or orthogonal) mxm matrix Q and an upper-triangular m × n matrix R, that is, A = Q * R . Theory: a square mxm matrix Q is called unitary (or orthogona) if -,or equivalently,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT