Correlation matrix ignoring NaN
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Star Strider
el 13 de Abr. de 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 comentarios
dpb
el 13 de Abr. de 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 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!