## Loren on the Art of MATLABTurn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

# No Infs, NaNs, or Bits

You may have heard the phrase "no ifs, ands, or buts" - meaning that there whatever the issue is, there is a definitive, non-negotiable answer or outcome. I heard it often when trying to persuade my parents to allow me to do something they didn't want me to do. Today when I heard someone use that phrase, what I sort of heard instead was "no |Inf|s, |NaN|s, or bits". So I will talk about those values and types today.

### Contents

In many applications, an infinite result is not acceptable. I do remember a quantum physics final exam question where I did end up with Inf, but then realized I needed to take the arctangent (atan2), and all was good with my answer. How do we detect the presence of infinity? Using one of the many is* functions!

Let me throw a bunch of values into an array so we can check them for various values or conditions.

vals = [-Inf 0 pi 17 42 Inf NaN]

vals =
-Inf         0    3.1416   17.0000   42.0000       Inf       NaN


Notice that isinf correctly finds positive and negative infinite values. First I see which ones are infinite, then I select them for display, via logical indexing.

nonInfVals = isinf(vals)
myInfVals = vals(nonInfVals)

nonInfVals =
1     0     0     0     0     1     0
myInfVals =
-Inf   Inf


Instead, I can just find the finite values.

finiteVals = vals(isfinite(vals))

finiteVals =
0    3.1416   17.0000   42.0000


For a nice use of -Inf in an algorithm, check out this post, where a -Inf results indicate that no solution smaller than the threshold is available.

#### And now for the NaNs

And, of course, there's my longstanding friend isnan, a great tool for selecting the NaN values, and along with logical indexing, replacing or removing them.

replacedNaNvals = vals;
replacedNaNvals(isnan(replacedNaNvals)) = -999

replacedNaNvals =
-Inf         0    3.1416   17.0000   42.0000       Inf -999.0000


#### Bits

MATLAB does have some functions for operating on arrays bit-wise, but does not have a bit datatype.

While MATLAB doesn't have a bit datatype, it does have some integer types. Let's see how we might use this. I will create a cell array with different types in each cell so we can explore a bunch of types in one call.

mycell = {magic(2) uint8(magic(2)) 'buts'; ...
int32(magic(3)) [Inf -Inf] NaN; ...
logical(eye(2)) {magic(2)} {uint8(magic(2))}}

mycell =
[2x2 double ]    [2x2 uint8 ]    'buts'
[3x3 int32  ]    [1x2 double]    [     NaN]
[2x2 logical]    {1x1 cell  }    {1x1 cell}


Let's see what isinteger can tell us.

integerCells = cellfun(@isinteger, mycell)

integerCells =
0     1     0
1     0     0
0     0     0


Notice that this function, isinteger, essentially works on each separate array, rather than elementwise, because it is answering a question about the type of the array, not individual values. Some is* functions act elementwise, and others act on an array as a whole.