Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need to answer whenever it says \"now its your turn\". Thank you so mcuh Activ

ID: 3821041 • Letter: I

Question

i need to answer whenever it says "now its your turn". Thank you so mcuh

Activity 1: Fun with 2-D Array Basics: Magic Squares The MATLAB function magic (N) constructs an NXN magic square. magic (3) ans The "magical" property of a magic square is that it contains the integers l to N2 exactly once each and every row, column, and diagonal has the same sum: 8-1 6 15 3+5+7 15 4+9+2 15 8+3+4 15 1+5+9 15 6+7+2 15 8+5+2 15 6+5+4 15 Now it's your turn. Write a function (verify magic.m that that take as input a square matrix and computes the sum of each row, column, and diagonal of the matrix to determine whether the input is a magic square. If the input is not a square matrix, use the error function to display an error message and abort the function. The output of the function should be a logical value indicating whether or not the input is a magic square: logical true for a magic Square, logical false for a muggle square. Example: verify magic (magic (3) ans verify ma 1 2 3 4 5 6 7 8 9]) ans

Explanation / Answer

function isMagic = verify_magic(mat)
n = size(mat, 1);
m = size(mat, 2);
if n ~= m
error("matrix should be square");
isMagic = false
return
else
mat_check = zeros(n*n, 1);
  
for i = 1:n
for j = 1:n
mat_check(mat(i, j)) = 1;
end
end
  
  
for i = 1: n*n
if mat_check(i) == 0
isMagic = false;
return
end
end
  
row_sum = 0;
for j = 1:n
row_sum = row_sum + mat(1, j);
end
for i = 2:n
rsum = 0;
for j = 1:n
rsum = rsum + mat(i, j);
end
if row_sum ~= rsum
isMagic = false;
return
end
end
  
diag_sum = 0;
for i = 1:n
diag_sum = diag_sum + mat(i, i);
end
  
if diag_sum ~= row_sum
isMagic = false;
return
end
  
diag_sum = 0;
for i = 1:n
diag_sum = diag_sum + mat(i, n+1-i);
end
  
if diag_sum ~= row_sum
isMagic = false;
return
end
isMagic = true;
return
end
end

% one question at a time please.