Given any square matrix A of size 3 times 3 or larger, return a square matrix B
ID: 2080077 • Letter: G
Question
Given any square matrix A of size 3 times 3 or larger, return a square matrix B of the same size as A where all the edge cells are equal to 0 and all other cells are equal to adding together the 8 cells that touch it. If A = [1, 2, 3; 4, 5, 6; 7, 8, 9] then you should produce B = [0, 0, 0; 0, 40, 0; 0, 0, 0] Instructions: To solve this problem, modify the template bellow with your code. Leave the name of the function and variables unchanged. Otherwise your code will not pass the test. Click Test to test your solution before submitting it. When you are satisfied with it click Submit.Explanation / Answer
Ans)
Matlab code)
==========
function B = addCells(A)
%ADDCELLS Summary of this function goes here
% Detailed explanation goes here
[r,c]=size(A);
B=A;
B(1,:)=0;
B(:,1)=0;
B(end,:)=0;
B(:,end)=0;
for i=2:(r-1)
for j=2:(c-1)
B(i,j)=A(i+1,j)+A(i-1,j)+A(i,j+1)+A(i,j-1)+A(i-1,j+1)+A(i-1,j-1)+A(i+1,j+1)+A(i+1,j-1);
end
end
end
===============================
The output when you run
=======
>> A=[1 2 3;4 5 6;7 8 9];
>> addCells(A)
ans =
0 0 0
0 40 0
0 0 0
>>
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.