function subs = ndx2subs(ndx,mrows)
%
% Given a vector <ndx> of vector indices, & no. of rows,
% it returns an array of the equivalent subscripts.
%
if (nargin == 2)
  [m n] = size(ndx);
  if m < n
    ndx = ndx';
  end
  if ~isempty(ndx)
    x = rem(ndx,mrows);       % convert vector indices to row subscripts
    tmp = find(x == 0);
    x(tmp) = x(tmp) + mrows;  % row 0 (from modulus operation) should be row m
    y = (ndx - x) / mrows + 1;% convert vector indices to column subscripts
    subs = [x' ; y']';        % put row and column subscripts into result matrix
  else
    subs = [];
  end
else
   error('Invalid number of arguments.')
end;
