How do I code this in a more efficient way?
Show older comments
The prompt states:
Let matrix M (with R rows, C columns) represent the number of bacteria in each block of this grid. When a drop of antibiotic is delivered to a specific block at row x and column y, it kills all of the bacteria on that block and half of the bacteria in all of the neighboring blocks. After the antibiotic is delivered to the block at 2nd row and 3th column, it kills all of the 10 bacteria on that block and half of the bacteria in neighboring blocks: (2+3+13+11+8+7+6+12)/2=31; killing a total of 41 bacteria.
Write a function killbacteria(M,x,y) that returns the total number of bacteria killed.
_________________
In my function I tried creating a new matrix with the adjusted bacterial cells, but I don't know how to make it so that it avoids going into non-existent rows and columns when it is dropped in the first row/column for instance.
Here's my code so far:
function out= killbacteria(M,x,y)
out=M;
out(x,y)=0;
if x>=0 && y>=0
out(x,y-1)=1/2*M(x,y-1);
out(x,y+1)=1/2*M(x,y+1);
out(x+1,y)=1/2*M(x+1,y);
out(x-1,y)=1/2*M(x-1,y);
out(x+1,y+1)=1/2*M(x+1,y+1);
out(x-1,y-1)=1/2*M(x-1,y-1);
out(x-1,y+1)=1/2*M(x-1,y+1);
out(x+1,y-1)=1/2*M(x+1,y-1);
else
fprintf('invalid');
end
Accepted Answer
More Answers (0)
Categories
Find more on Gravitation, Cosmology & Astrophysics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!