A two dimensional square array is considered to be symmetric if for every row (r
ID: 3554744 • Letter: A
Question
A two dimensional square array is considered to be symmetric if for every row (r) and column(c) index/subscript Arc = Acr. For example in the following two arrays A is symmetric because A12 = A21, A13 = A31 and A23 = A32 whereas B is not symmetric because B13 does not equal B31. Create a MATL AB function that will accept a two-dimensional square and the function input and will send back true or false to the function call. The function should employ a nested loop to compare elements of the array and determine if the array is symmetric or not. The function may not include any built-in function other than the length (also remember that break and continue are not permitted to be used in this course), you will receive an automatic zero for this problem if you fail to follow this instruction. Note MATL AB has a predefined value of 1 for the word true and 0 for the word false that you may find useful. In the script file, use the input function to prompt the user to enter a square 2-dimensional array. Use a loop to check that user's input is a square array and repeatedly ask the user to enter a square array until they enter a square array (you may use the size function for this check). Once a square array the script file should call the function with the square array. Then the script file should output the array and a message if it was symmetric or not.Explanation / Answer
Script File :
check=0;
while check==0
display('Input the Square array as a list of comma seperated value : ');
A = input('example : [1,2;3,4] : ');
[m n]=size(A);
if m==n
check=1;
else
display('Dimension of array is not correct, please re-enter the matrix')
end
end
result = isSquareMatrix(A);
disp('1 implies the matrix is symmetric and 0 implies matrix is not symmetric.');
display('Result');
disp(result);
Function : isSymmetric.m
function [ bool ] = isSymmetric.m( A )
bool=true;
n=length(A);
for i=1:n
for j=i:n
if A(i,j)~=A(j,i)
bool=false;
return
end
end
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.