Loren on the Art of MATLAB

Turn 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

Talking about infinity

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.

Casting for an answer

There are a few more interesting functions in the functions covering numeric types, particularly, typecast and cast. typecast allows you to convert data types without changing the underlying data. cast lets you cast the variable to a different datatype. And swapbytes let's you swap the byte ordering, particularly useful for transforming the endianness of data.

My bet...

No ifs ands or buts - I am sure many of you already make good use of a few of these Infs, NaNs, and bit functions. What kind of data value wrangling do you do with MATLAB? I mean things like I've mentioned in this post, not as "big" as peak or outlier detection. MATLAB and add-on products do have many of those capabilities. I'm looking for the "little" things. Let me know here.




Published with MATLAB® R2015b


  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.