What is the reasoning behind the fact that min(0,NaN) is 0?

I know that Mathworks pays a lot of attention to this stuff, so I am wondering why the expression
>> min(0,NaN)
is 0. Returning a NaN here seems more logical to me.

댓글 수: 4

Bryan
Bryan 2020년 4월 25일
편집: Bryan 2020년 4월 25일
It really doesn't seem to make sense at all, in terms of consistency within the Matlab environment:
NaN + 1 + 3 = NaN
NaN * 1 / 3 = NaN
sum([NaN 1 3]) = NaN
nansum([NaN 1 3]) = 4
mean([NaN 1 3]) = NaN
nanmean([NaN 1 3]) = 2
std([NaN 1 3]) = NaN
nanstd([NaN 1 3]) = 1.4142
median([NaN 1 3]) = NaN
nanmedian([NaN 1 3]) = 2
min([NaN 1 3]) = 1
nanmin([NaN 1 3]) = 1
max([NaN 1 3]) = 1
nanmax([NaN 1 3]) = 1
Is there actually any other basic function in Matlab that ignores NaN by default?
Been using Matlab for 10 years and only discovered this now when we had a bug in a published script that was due to this. So sure, apparently it is in the documentation for min() and max() that those basic functions of Matlab decide to ignore the established handling of NaN within the Matlab environment. I had not read that and taken for granted that NaN would bet treated as it is everywhere else in Matlab. I realise I should have read the documentation, but I guess I just didn't expect such inconcistency.
I guess a workaround in my case is to use one of the functions that treats NaNs correctly to index where NaN should be returned (I was doing this on a 2 by 1 by 10^6 matrix), and then replace all those indices in the min() output with NaN. Or the 'includenan' flag
"...the 'includenan' on min() also has strange behaviour:"
min([NaN; 2; 3; NaN],1,'includenan')
ans =
NaN
1
1
NaN
Why is that strange? You told MATLAB that you want the element-wise lowest values of the 1st or 2nd input, e.g. for the second element the minimum of 2 and 1 is 1... and for the first element the minimum of NaN and 1 is NaN (because of the includenan flag). It behaves exactly as the documentation describes. Perhaps you confused the min(A,B) syntax with specifying the dimension as the third input:
min([NaN; 2; 3; NaN],[],1,'includenan')
Bryan
Bryan 2020년 4월 25일
편집: Bryan 2020년 4월 25일
yes i noticed that very quickly (hence deleted that bit of the post). but you were in there very quick with helpful feedback, thanks for the explanation.
As a small bit of feedback, perhaps the min() documentation could mention in the very first line of the documentation that NaN values will be ignored by default, since this is unusal behaviour in the Matlab environment. The documentation here: https://se.mathworks.com/help/matlab/ref/min.html and in 'help min' states
M = min(A) returns the minimum elements of an array.
M = min(X) is the smallest element in the vector X.
perhaps it could state
M = min(A) returns the minimum non-NaN elements of an array.
M = min(X) is the smallest non-NaN element in the vector X.
or alternatively
M = min(A) returns the minimum elements of an array, whereby NaN elements are ignored by default.
M = min(X) is the smallest element in the vector X, whereby NaN elements are ignored by default.
cheers
Stephen23
Stephen23 2020년 4월 25일
편집: Stephen23 2020년 4월 26일
@Bryan: you should make that as an enhancement request.
Another option is to stop relying on inconsistent "default" behavior and always specify any flags, dimensions, etc. for any function that has these kind of options. Although it requires a little more typing, it has the following advantages:
  • makes the intention clear
  • avoids bugs, e.g. when a matrix ony has one row (and thus min returns a scalar, not a row vector)
  • throws an error on versions that do not support that option, rather than silently continuing...

댓글을 달려면 로그인하십시오.

답변 (5개)

Walter Roberson
Walter Roberson 2012년 5월 21일

3 개 추천

If you initialize the result to inf, and then loop testing whether the current value is less than the result and replace the result if it is, then since NaN < any number is false, the result will never get replaced with NaN. You would have to add special code to return NaN in such a case.
Sean de Wolski
Sean de Wolski 2012년 5월 21일

2 개 추천

At the bottom of the doc page:
The min function ignores NaNs

댓글 수: 2

Sean, I appreciate the reply. I realize that the documentation informs me THAT the function will ignore NaNs. That does not enlighten me as to WHY that choice was made, which is what I am trying learn.
It depends on how you understand the MIN function. 0 < NaN replies FALSE, but NaN < 0 replies FALSE also. As long as it is well documented, both values are reasonable.

댓글을 달려면 로그인하십시오.

Daniel Shub
Daniel Shub 2012년 5월 22일

2 개 추천

Given the behavior of MIN, I find it odd that there is a NANMIN function.

댓글 수: 2

That is really interesting. If you look inside nanmin, it has one line.:
[varargout{1:nargout}] = min(varargin{:});
MIN and MAX ignores NaN. MEAN and SUM does not. I guess NANMIN (in stat toolbox) is for people like me who cannot remember all the details when we cannot see the underlying logic.

댓글을 달려면 로그인하십시오.

M Sohrabinia
M Sohrabinia 2012년 6월 21일

0 개 추천

NaN is considered undefined, so undefined is ignored by most functions (0/0 will be resulted in NaN which is basically undefined but any number divided by 0, say 4/0, will result in inf). However, the question is why Matlab has decided to treat NaNs in a certain way in some functions, e.g., sort function will always arrange NaNs at the top end (A to Z mode). I guess Matlab has just decided to adopt some rules to handle exceptions.
Mark vanRossum
Mark vanRossum 2021년 6월 3일

0 개 추천

I encountered this when working on arrays.
x=[1 NaN 10];
y=[5 5 5];
m=min(x,y) and m=nanmin(x,y) return [1,5,5]
In V2020, min(x,y,'includenan') returns [1, NaN,5]
Here is an ugly workaround to get the desired behaviour in older versions.
q=isnan(x)
m=min(x,y)
m(q)=NaN

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2012년 5월 21일

답변:

2021년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by