Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inf or Inf? nan or NaN?

Tags:

matlab

It seems like both inf and Inf are exactly same in MATLAB (likewise nan and NaN, but not Nan). Is there any difference?

>> which inf
built-in (/Applications/MATLAB_R2011b.app/toolbox/matlab/elmat/inf)
>> which Inf
built-in (/Applications/MATLAB_R2011b.app/toolbox/matlab/elmat/Inf)

If they are the same, which one should be used in practice? For allocating arrays I have been using x = inf(3,5) style following zeros and ones being all small caps. For assigning single value, I use x = Inf. Do you think this is a consistent use?

like image 643
Memming Avatar asked Jan 10 '13 22:01

Memming


2 Answers

Most idiomatically consistent would be nan and inf but MATLAB offers you the alternative way of capitalizing NaN and Inf, the way you will find it everywhere else, like in printouts, for example. Note that MATLAB is case sensitive. Nobody will use Nan or InF, so MATLAB does not provide these "aliases".

EDIT: For use in a data vector, as in [3.7, 1.2, NaN, 3.1], I consistently find myself using NaN as well, but the following experiment suggests very strongly that this use is not intimate with MATLAB's internal workings. Create a function n = NaN() returning 4 and save it as NaN.m in your current folder. Defining the vector like above will result in [3.7, 1.2, 4.0, 3.1] showing that MATLAB does not understand NaN as a constant, and will look up a function, which, in accordance with MATLAB idioms, should be spelled all lowercase.

Now let's quickly delete NaN.m before we forget, and keep using NaN in data columns.

like image 123
s.bandara Avatar answered Nov 07 '22 19:11

s.bandara


Here are the conventions that MATLAB appears to use:

For Not-a-Number: Always use NaN (Except in combinations such as isnan()

For Infinite: Use inf for the function and use Inf for the value (and INFs for multiples, but this is not a command of course). Note that this is a bit tricky as it means that the evaluation of inf gives Inf.

Deduced from:

help Inf: inf(N) is an N-by-N matrix of INFs.

help nan: NaN(N) is an N-by-N matrix of NaNs.

help isnan: For example, isnan([pi NaN Inf -Inf]) is [0 1 0 0].

like image 20
Dennis Jaheruddin Avatar answered Nov 07 '22 20:11

Dennis Jaheruddin