%**********************************************************
%   This function computes the correlation coefficient 
%   between two vectors x and y.
%   AUTHOR  : Balu Santhanam
%   DATE    : 05/19/99
%   USAGE   : rho_xy = cor_coeff(x,y)
%   EXAMPLE : x = [0:0.1:3]; y = 3*x + 4;
%             rho_xy = cor_coeff(x,y) = 1
%**********************************************************
function rho_xy = cor_coeff(x,y)
%  Error checking
if nargin ~= 2 
   error('Insufficient Input Information')
elseif length(x)==0 | length(y)==0
   error('Null Input')
elseif length(x) ~= length(y)
   error('Vectors need to have same dimension')
elseif any(x)==0 | any(y)==0 
   error('Input is a zero vector')
elseif isnumeric(x) ~= 1 | isnumeric(y) ~= 1
   error('Input is non-numeric')
elseif all(isfinite(x)) == 0 | all(isfinite(y)) == 0
   error('Input contains Inf/NaN values')
else
%  Coefficient computation
   sig_x = std(x); sig_y = std(y); n = length(x);
   cov_xy = (1/(n-1))*dot(x-mean(x),y-mean(y));
   rho_xy = abs(cov_xy / (sig_x*sig_y));
end
%******************************************************** 
