clc clear all % special matrices disp('Example 3 - Identity matrix?') % check the diagonal elements for = 1 % check the non-diagonal elements for = 0 % INPUTS a=[1 0 0; 0 1 0; 0 0 1]; disp('Input matrix') disp(a) % CODE [m,n]=size(a); if m~=n disp(' Error - Matrix is not square') else sq_count=0; % starting point for non-diagonal id_count=0; % starting point for diagonal for i=1:1:m for j=1:1:n % check the diagonals if a(i,j)==1 & i==j id_count=id_count+1; end % check the non-diagonals if a(i,j)==0 & i~=j sq_count=sq_count+1; end end end if sq_count==n^2-n & id_count==n disp('This is an identity matrix') else disp('This is not an identity matrix') end end