function tf = isDiagonal(A)
%ISDIAGONAL Is a square matrix a diagonal matrix?
%   tf = isDiagonal(A) returns true if A is a diagonal matrix
%   and false otherwise.

% to solve this problem you need to look at the off-diagonal
% elements of the matrix, if you find an off-diagonal element
% that is not equal to zero then A is not diagonal

% the diagonal elements of A have equal row and column indices
% so the off-diagonal elements have row and column indices that
% are not equal

[rows, cols] = size(A);
tf = true;
for r = 1:rows
    for c = 1:cols
        if r ~= c
           if A(r, c) ~= 0
               tf = false;
               break;   % not necessary, but more efficient
           end
        end
    end
end
