File Exchange

## nancorr

version 1.0.0 (1.68 KB) by Oleksandr Frei

### Oleksandr Frei (view profile)

Calculates Pearson correlation allowing for nan values.

Updated 20 Jun 2019

nancorr(A, B) is equivalent to matlab's corr(A, B, 'Rows', 'pairwise'), except nancorr routine is orders of magnitude faster on large matrices.

nancorr also returns t-statistics:
[coef, t] = nancorr(A, B); zmat = t;

These can be converted to p-values as follows:
[coef, t, n] = NANCORR(A, B);
pval = tcdf(-abs(t), n - 2)

### Cite As

Oleksandr Frei (2020). nancorr (https://www.mathworks.com/matlabcentral/fileexchange/71893-nancorr), MATLAB Central File Exchange. Retrieved .

Michal Kvasnicka

### Michal Kvasnicka (view profile)

MATLAB R2020a ... nancorr is no more faster than corrcoef!!!

>> s = rand(1e5,10);
>> tic;w = corrcoef(s,'rows','pairwise');toc
Elapsed time is 0.003665 seconds.

>> tic;w_ = nancorr(s,s);toc
Elapsed time is 0.015015 seconds.

Moreover, nancorr is numerically unstable (see Steve Smith note)!!!

Steve Smith

### Steve Smith (view profile)

Excellent - thanks. One minor buglette fix is below: this stops underflow/overflow in calculations from causing the output to become complex (which happened to me with my data):
% sx, sy - standard deviations
% sx = sqrt(x2 - n .* (mx.^2));
% sy = sqrt(y2 - n .* (my.^2));
sx = x2 - n .* (mx.^2); sx(sx<0)=0; sx=sqrt(sx);
sy = y2 - n .* (my.^2); sy(sy<0)=0; sy=sqrt(sy);

steve@fmrib.ox.ac.uk

Chang hsiung

### Chang hsiung (view profile)

##### MATLAB Release Compatibility
Created with R2019a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux