function mattrans = mytranspose(mat)
% Transposes a matrix as a demonstration of the use of nested for loops
% for array operations.
%
% Input argument: mat --- m x n matrix (2-D array)
%
% Output argument: mattrans ---- n x m matrix, transpose of mat

   % Determine dimensions of array: recall that size(a) returns 
   % a row vector of the dimensions.
   sizemat = size(mat);

   % First element is the number of rows.
   m = sizemat(1);

   % Second elemnt is the number of columns.
   n = sizemat(2);

   % Create the output array, initialized to zero.
   % Preallocating an array in this way is more efficient
   % than "growing" the array element by element.
   mattrans =  zeros(n, m);

   % Loop over all elements of mat using nested for's
   for ii = 1 : m
      for jj = 1 : n
         % Transpose individual elements ...
         mattrans(jj, ii) = mat(ii, jj);
      end
   end

end
