nanmean returns -Inf
Mostra commenti meno recenti
Vector A is 7627x1 vector with 150 values, 10 of which are negative, and the rest are NaNs.
Calling nanmedian(A) returns a real number value ( 2.7462). Calling nanmean(A) returns " -Inf". Why does nanmedian return a real value and nanmean does not? How do I fix it to get a real value returned from nanmean?
Of interest may be how I am arriving at vector A:
A=(X1./Y1)./(X2./Y2);
I also tried a for loop unsuccessfully. It returned the same results as the non-for loop version above:
for i=1:length(X1)
A(i)=(X1(i)./Y1(i))./(X2(i)./Y2(i));
end
X1 and X2, are 7627x1, mostly negative real numbers. Y1 and Y2, are 7627x1, all positive real numbers.
2 Commenti
balsip
il 5 Set 2017
Walter Roberson
il 5 Set 2017
nanmean for a vector x is the same as
t = x(~isnan(x));
result = mean(t)
so if there is an +/- inf in the data then it is not affected by the removal of the nans . mean() of data that includes +/- inf is +/- inf if all of the inf are the same sign and nan otherwise. Only a single +/- inf is needed to have an infinite sum and so an infinite mean.
nanmedian of the vector x is the same as
t1 = x(~isnan(x));
t2 = sort(t1);
if mod(length(t2), 2) == 0
result = 1/2 * (t2(end/2)+t2(end/2+1))
else
result = t2( ceil(end/2) );
end
This will produce +/- inf only if at least half of the values are +/- inf . In the case of a vector with the same number of +inf and -inf and no other values, the result could be nan due to the attempt to average the -inf and +inf that would then be the two central elements. At least half infinite is needed in order for the middle elements after sort to end up being infinite for an infinite result.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Code Performance in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!