Correlation matrix ignoring NaN

3 次查看(过去 30 天)
V
V 2015-4-13
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)/

回答(2 个)

Star Strider
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');

dpb
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,:)=[];

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by