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

### the cyclist (view profile)

on 21 May 2012

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.

## Products

### Sean de Wolski (view profile)

on 21 May 2012

At the bottom of the doc page:

doc min

The min function ignores NaNs

the cyclist

### the cyclist (view profile)

on 21 May 2012

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.

Jan Simon

### Jan Simon (view profile)

on 22 May 2012

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.

### Walter Roberson (view profile)

on 21 May 2012

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.

### Daniel Shub (view profile)

on 22 May 2012

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

Jonathan Sullivan

### Jonathan Sullivan (view profile)

on 21 Jun 2012

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

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

per isakson

### per isakson (view profile)

on 21 Jun 2012

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 (view profile)

on 21 Jun 2012

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.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi