function mattrans = mytranspose(mat)
% mytranspose Transposes a matrix using nested for loops and elementwise ops [PHYS210-DEMO]
%
% This functions demonstrates elementwise operation on a matrix
% (2D array) using nested for loops.
%
% Input argument: mat --- m x n matrix (2-D array)
%
% Output argument: mattrans ---- n x m matrix, transpose of mat

   % Determine the dimensions of the input matrix: and assign them
   % to m and n. 
    
   % Recall that size(a) returns a row vector containing the 
   % dimensions of the array a: in this case the number of 
   % rows and the number of columns.

   [m, n] = size(mat);

   % Create the output array, initialized to zero.
   % Preallocating an array in this way is more efficient
   % than "growing" the array element by element. The input 
   % array is m x n, so its transpose is n x m.

   mattrans =  zeros(n, m);

   % Define all elements of mattrans using nested for loops.
   for ii = 1 : n
      for jj = 1 : m

         % Assign the individual elements of mattrans using the 
         % definition of the transpose operation.
         mattrans(ii, jj) = mat(jj, ii);

      end
   end

end
