function r=correlation(x,y)
% function correlation
% usage: r=correlation(x,y)
%
% calculates correlation between x and y
% x and y can contain NaN
% they can have 1, 2 or 3D
% but they must be the same size
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
% Copyright (C) 2006, Aida Alvera-Azcarate
% Author: Aida Alvera-Azcarate
mask=isnan(x) | isnan(y);
indy=find(~mask);%finds ones
x=x(indy);
y=y(indy);
mX=mean(x);
mY=mean(y);
xp=x-mX;
yp=y-mY;
r=sum(xp.*yp)/sqrt(sum(xp.^2)*sum(yp.^2));