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

Please help me to solve this MATLAB problem. I need all the steps. Inputs: 1. (d

ID: 1720179 • Letter: P

Question

Please help me to solve this MATLAB problem.

I need all the steps.

Inputs: 1. (double) An array of integers Outputs: 1. (logica) A logical representing whether the array is a magic square Function Description: As we are exploring the wonders of MATLAB, we find cooler and cooler things that it can do. One of the best things about MATLAB is that it can do magic! Wel not really...but it can produce a magic square on command with the magic function, which is pretty close. If you don't remember from your 4th grade math class, a magic square is a matrix of numbers where the rows, columns, and diagonals all add up to the same number. For example, the following is a valid magic square: 2 7 6 15 9 5 1 15 4 3 8 15 15 15 15 15 15 Write a function in MATLAB that takes in an array and checks whether the array is a magic square by outputting a logical. Remember the key aspects to check for a magic square: the array must be square, all elements must be unique, all columns and rows add to the same number, and both diagonals add to the same number as the rows and columns. Notes: You may not use the diag unique trace or eye() functions. A 1x1 matrix is a magic square. All numbers will be integers. Hints: Think about how you can use linear indexing to get the diagonal of the matrix without using any of the banned functions.

Explanation / Answer

First we need to define what we need to know so that the arrangement is a magic square:

Now we can generate a code of each of the above conditions.

Let A the array of numbers.

code:

D = size(A)

if D(1,1) = D(1,2)

'The matrix is square'

else

'The matrix is not square'

end

2. The numbers are integers.

Let A the array of numbers.

code:

if not(A) = zeros(size(A))

'All numbers in the array are integers'

else

'Some number in the array not are integers'

end

3. The sum of rows, columns and diagonals are equal.

code:

sum(A)

D = size(A)

n = D(1,1)

k = sum(A)/sizeD(1,1)

for i = 1:n

if sum(A(i,:)) <> k

'The array is not a magical square'

end

for i = 1:n

if sum(A(:,i)) <> k

'The array is not a magical square'

end

s = 0

for i = 1:n

s = s + A(i,i)

end

if s <> k

'The array is not a magical square'