问题描述:

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?

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

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.

您可能感兴趣的文章：

- php - mod_rewrite redirects to the CSS file
- ios - Show thumbnail for url attachment on UIActivityViewController
- jquery - Htaccess .php and trailing slash and hash portfolio filter
- xml - Get child nodes from Pointer in java
- Facebook Login for Android App with release key
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial