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
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
ADVERTISEMENT