. Create a 6 x 6 magic matrix called M using the magic function in MATLAB. a) Pa
ID: 3626333 • Letter: #
Question
. Create a 6 x 6 magic matrix called M using the magic function in MATLAB.a) Pass the matrix M to a function: function flag = CHECKMAGIC(M)
that will check if the sum of each row, the sum of each column and the sum of each diagonal are all
equal. The function should print the message: SQUARE IS MAGIC or SQUARE IS NOT MAGIC
b) If you square each element of M, is the new matrix a magic square? Print the matrix.
c) Extract and print a 3 x 3 matrix from the upper left corner of the magic matrix M. Is this also a magic
matrix?
Explanation / Answer
function checkmagic=checkmagic[m]
magic_num = 111; % this is sum of each row,col and diagonal
for i=1:6
if sum(m(:,i))~=111
disp('SQUARE IS NOT MAGIC');
return;
end
end
for i=1:6
if sum(m(i,:))~=111
disp('SQUARE IS NOT MAGIC');
return;
end
end
if sum(diag(m))~=111
disp('SQUARE IS NOT MAGIC');
return;
end
disp('SQUARE IS MAGIC');
return;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.