function [ rank, sort_ind ] = give_rank( x )
  
  if nargin < 1
    error( [ mfilename ' needs 1 input argument (matrix or vector).' ] );
  end
  
  size_x = size( x );
  
  % temporarily "flatten" the data into a matrix of column vector(s)
  
  if sum( size_x > 1 ) == 1
    % data is 1-D => single column vector
    x = x(:);
  else
    % data is N-D => matrix of column vectors
    x = reshape( x, size_x( 1 ), [] );
  end
  
  % Sort
  [ tmp, sort_ind ] = sort( x );
  
  s1 = size( sort_ind, 1 );
  s2 = size( sort_ind, 2 );
  
  rank = repmat( ( 1:s1 ).', 1, s2 );
  rank( sub2ind( size( rank ), sort_ind, repmat( 1:s2, s1, 1 ) ) ) = rank;
  
  % Back to the original size
  rank = reshape( rank, size_x );
  sort_ind = reshape( sort_ind, size_x );
  