% Spearman rank-sum correlation function
%           6 Sum (d(i)^2
% r = 1 - -----------------
%           n (n^2 - 1)

% Each row of x should be a sample, and each column is a
% variable.

function r = spearman (x)

   % Get the sizes and build the result matrix

   [nr, nc] = size (x);

   r = zeros (nc, nc);
   for v1 = 1:nc
      for v2 = 1:nc 

         % Pull off the two vectors, rank, subtract, square sum
         % and the calculate r.

         [y,rank1] = sort (x (:, v1));
         [y,rank2] = sort (x (:, v2));
         diff = sum ((rank1 - rank2).^2);
         temp = (6 * diff)/(nr * (nr^2 - 1));
         display (temp);
         r (v1, v2) = 1 - temp; 
      end
   end

