Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3866878 • Letter: #

Question

% 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.

Explanation / Answer

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)