Please solve the following MATLAB problem: Function Name: spellCheck Inputs 1. (
ID: 3890148 • Letter: P
Question
Please solve the following MATLAB problem:
Function Name: spellCheck Inputs 1. (char) An NxM array of characters Outputs 1. (logical) A logical representing whether the array is a magic square Banned Functions: diag() unique(), trace(), eye(), magic() Background It is up to you to help Ron complete his Charms homework. He needs to identify which spells are real, and which ones are made up. The trick is, all of the best spells can be rearranged into an array such that the ascii values will make a magic square! 27615 1-15 43815 As a reminder, a magic square is a matrix of numbers where the rows, columns, and diagonals all add up to the same number. Ron is able to rearrange the letters from the spell into an array, but since he is a wizard and he has no coding experience, he is counting on you to examine the arrays to figure out if they are magic or not. Function Description Write a function in MATLAB that takes in an array of characters and checks whether the corresponding ASCIl values create a magic square by outputting a logical. Remember the key aspects to check for a magic square 1) the array must be square 2) all elements must be unique 3) all columns and rows add to the same number 4) both diagonals add to the same number as the rows and columns Notes: A 1x1 matrix is a magic square Hints Think about how you can use linear indexing to get the diagonal of the matrix without using any of the banned functionsExplanation / Answer
function dis = spellCheck(A)
[m,n] = size(A);
count = 0;
pqr = 1;
if m ==n %checking if array is square matrix or not
q = true;
end
if q
B = unit8(A); %converting char array into ascii array
for t = 1:m
for u = 1:n
count =0;
for v = 1:m
for w = 1:n
if B(t,u) ==B(v,w)
count++;
if(count>1)%checking whether the elements in matrix are unique or not
pqr = 0;
break;
else
pqr = 1;
end
end
end
if(count>1)
break;
end
end
if(count>1)
break;
end
end
if(count>1)
break;
end
end
end
%calculating sum for diagonal elements
sum = 0;
for a = 1:m
for b = 1:m
if a==b
sum = sum + B(a,b);
end
end
end
%calculating sum for rows
for a = 1:m
sum1=0
for b=1:m
sum1 = sum1 + B(a,b);
end
if(sum1 == sum)
flag =1;
else
flag = 0;
break;
end
end
% calculating sum for columns
for a = 1:m
sum2 = 0
for b = 1:n
sum2 = sum2 + B(b,a);
end
if(sum2 == sum)
flag =1;
else
flag = 0;
break;
end
end
disp(flag);%output the result 1/0 (logical value)
end