I am getting NaNs when I use perform correlation between two matrices, but I want to ignore them, and I read on some MATLAB forum that replacing NaNs with the means obtained by nanmean is a pretty good replacement. So I executed the following code, but Im still getting NaNs in my result. Why is that happening?
cor1nan=nanmean(correlations1);
mod1cor=isnan(correlations1);
for z=1:264
for a=1:264
if(mod1cor(a,z)==1)
correlations1(a,z)=cor1nan(z);
end
end
end
Just noticed that some of the values in cor1nan are NaNs themselves.. how I go about solving this?
If you have columns that are all
NaN, thennanmeanwill keep it asNaNif I read it correctly. Then your loop below will substitute in aNaN, thus keeping them.A suggestion for how to solve it is as follows. Replace your code with:
This will replace the
NaNs incor1nanwith the mean of the non-NaNvalues incor1nan