Correlation matrix ignoring NaN
10 views (last 30 days)
Show older comments
Hi guys,
I have the attached matrix and I am trying to compute pairwise correlations between columns but I want to ignore all the columns which have a single NaN (i.e. the result for any pair of columns in which at least one entry is NaN should be NaN).
Here's my code so far:
for i=1:size(auxret,2)
for j=1:size(auxret,2)
rho(i,j)=corr(auxret(:,i),auxret(:,j));
end
end
end
But this is extremely innefficient. I considered using the function:
corr(auxret, 'rows','pairwise');
But it didn't produce the same result (it ignores NaNs but still computes the correlation - so unless all entries of a column except one are NaN it will still give an output)/
0 Comments
Answers (2)
Star Strider
on 13 Apr 2015
If you want a NaN result for all rows and columns that contain NaN values, either use the default, or specify:
R = corr(auxret, 'rows','all');
0 Comments
dpb
on 13 Apr 2015
IIUC, any column containing any NaN shouldn't be included so reduce the matrix first...
auxret(any(isnan(auxret)),:)=[];
If the original columns are important then you'll want to keep an index so that know which have been removed...
idx=any(isnan(auxret));
auxret(idx,:)=[];
0 Comments
See Also
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!