Correlation matrix ignoring NaN
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Star Strider
2015-4-13
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 个评论
dpb
2015-4-13
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!