Given a square matrix M of odd dimensions (3 by 3.5 by 5 or larger in general):
ID: 3803771 • Letter: G
Question
Given a square matrix M of odd dimensions (3 by 3.5 by 5 or larger in general): Write a MATLAB function [centerdiamond] = centerdiamond(M) to make zero the elements of the matrix as shown in the example. The rest of the entries in the matrix should remain unchanged. You should preserve the original matrix. Use any method. Your code should work in general with any square matrix of odd dimensions. 1 -3 2 4 2 5 7 8 9 5 3 2 4 6 3 6 8 2 1 3 3 3 3 3 3 Becomes: 1 -3 0 4 2 5 0 8 0 5 0 2 4 6 0 6 0 2 0 3 3 3 0 3 3 1 -3 2 4 2 5 3 5 7 8 9 5 3 2 3 2 4 6 3 4 1 6 8 2 1 3 5 4 3 3 3 3 3 6 3 2 2 2 2 2 2 7 3 5 6 1 4 6 2 Becomes: 1 -3 2 0 2 5 3 5 7 0 9 0 3 2 3 0 4 6 3 0 1 0 8 2 1 3 5 0 3 0 3 3 3 0 3 2 2 0 2 0 2 7 3 5 6 0 4 6 2Explanation / Answer
function [res]=SquareMatrix()
%if the values have diferent sizes or classes replace [] with {}
sm=input('Enter the dimesion : ');
x=(sm+1)/2;
A = magic(sm)
%A(logical(eye(size(A)))) = 0
[nRows,nCols] = size(A);
A(x:(nRows+1):nRows*nCols) = 0
A(nRows:x:nRows*nCols) = 0
A(nCols:x:x) = 0
%SE=strel('diamond',sm)
end
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.