Documentation of corr2


Global Index (all files) (short | long) | Local Index (files in subdir) (short | long)


Function Synopsis

r=corr(vect1, data, lag);

Help text


   r=corr(vect1, data, lag);

   r is the correlation between the two input vectors

   lag is the number of points that vect1 should lag
     data.  For instance:

        corr(a(1:10), b(1:10), 1) will produce the same as:
        corr(a(2:10), b(1:9))

   If lag < 0, then data will lag vect1 by abs(lag) 
     number of points.


Cross-Reference Information

This function is called by

Listing of function corr2

function r=corr(vect1, data, lag);

if nargin < 3;
  lag = 0;
end;

[m,n]=size(vect1);
if m == 1;
  vect1 = vect1';
  [m,n] = size(vect1);
end
[l,k]=size(data);
if m ~= l;
  if m == k;
    data = data';
    [l,k] = size(data);
  else
    error('Number of time points must be equal');
  end
end

if lag < 0;
  tem = vect1;
  vect1 = data;
  data = tem;
  clear tem;
  lag = abs(lag);
end

ind1 = (lag+1):l;  ind2 = 1:(l-lag);
v1 = (vect1(ind1,:) - (ones(length(ind1),1)*mean(vect1(ind1,:))));
v2 = (data(ind2,:) - (ones(length(ind2),1)*mean(data(ind2,:))));

if n == 1;
  r = (v1' * v2) ./ ((length(ind1)-1)*std(v1).*std(v2));
elseif n == k;
  for i = 1:k;
    r(i) = (v1(:,i)' * v2(:,i)) ./ ((length(ind1)-1)*std(v1(:,i))*std(v2(:,i)));
  end
else
  error('vect1 must be a column vector, or must be the same size as data');
end