Correlation matrix ignoring NaN
조회 수: 4 (최근 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!