Getting 'NaN' in an Array or Array Calculations

Updated Aug 24, 2023

Reported In

Software

  • LabVIEW Full
  • LabVIEW Base
  • LabVIEW Professional
  • LabVIEW Real-Time Module

Issue Details

I am trying to find the mean of an array. But, keep getting 'NaN' as my result.

Solution

Not a Number (NaN) is given when trying to perform calculations on part of an array that is not initialised. For example, if you have the following array:

If you try and find the mean of this array then you will find that the results will be NaN. This is because it is trying to find the mean value of nothing. It is not because the elements are equal to zero but because they are not numbers. When you create an array control or constant, there are no values assigned to the elements in the array causing LabVIEW to read NaN.

Additional Information

If you have an array that has elements that are NaN and you would like to remove them so that you can perform operations such as finding the mean or standard deviation of a column or row then you can remove these elements and replace them with zeros by using the 'Not a Number/ Path/ Refnum?' function. The following code will remove any cases of NaN and replace them with zeros:

In general, if you find NaN appearing in arrays or as results of functions, take a look at the data you are trying to read or manipulate.