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

8 views (last 30 days)

Show older comments

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 Comments

Bryan
on 25 Apr 2020

Edited: Bryan
on 25 Apr 2020

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(X) is the smallest element in the vector X.

perhaps it could state

M = min(X) is the smallest non-NaN element in the vector X.

or alternatively

M = min(X) is the smallest element in the vector X, whereby NaN elements are ignored by default.

cheers

Stephen23
on 25 Apr 2020

Edited: Stephen23
on 26 Apr 2020

@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...

### Answers (5)

Walter Roberson
on 21 May 2012

##### 0 Comments

Sean de Wolski
on 21 May 2012

##### 2 Comments

Jan
on 22 May 2012

Daniel Shub
on 22 May 2012

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

##### 2 Comments

Jonathan Sullivan
on 21 Jun 2012

That is really interesting. If you look inside nanmin, it has one line.:

[varargout{1:nargout}] = min(varargin{:});

per isakson
on 21 Jun 2012

M Sohrabinia
on 21 Jun 2012

##### 0 Comments

Mark vanRossum
on 3 Jun 2021

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

##### 0 Comments

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!