LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I remove NaN values from an array?

Solved!
Go to solution

I'm trying to test if the values in an array are less than 0.001. All of them are...BUT the problem is that some of the elements in the array are NaN. I'd like to do one of two things:

 

1. Remove the NaN elements from the array and set them to zero to make the test work.

2. Make the test understand that NaN elements are okay.

 

The test results in a boolean array of T/F values. If all of the values of the boolean array are T, it will result in a single boolean value of T. In #2, I am saying that I want it to test if an element of the array is less than 0.001 OR equal to NAN.

0 Kudos
Message 1 of 6
(3,170 Views)
Solution
Accepted by topic author twolfe13

 


@twolfe13 wrote:

I'm trying to test if the values in an array are less than 0.001. All of them are...BUT the problem is that some of the elements in the array are NaN. I'd like to do one of two things:

 

1. Remove the NaN elements from the array and set them to zero to make the test work.


 

There have been various posts in the past on removing elements from an array. I'm sure you took the time to search before you posted, right? In any event, it does not appear that order matters here, so the easiest way to do this is to sort the array, and then search for the first occurrence of NaN. Just do a comparison on the subset of the array prior to this index.

 

 


2. Make the test understand that NaN elements are okay.

 

The test results in a boolean array of T/F values. If all of the values of the boolean array are T, it will result in a single boolean value of T. In #2, I am saying that I want it to test if an element of the array is less than 0.001 OR equal to NAN.


Use the Or Boolean function and the Not a Number/Path/Refnum function.

 

0 Kudos
Message 2 of 6
(3,164 Views)

Got #2 to work. Actually on #1 it is a little more complicated than that. I could potentially have n-number of NaN values in my array that I want to move. I did see how to remove NaN once, but couldn't determine a good way to generalize it other than doing a test loop. I thought there might have been a simple function that I overlooked to do this.

0 Kudos
Message 3 of 6
(3,159 Views)

Have you look at the coerce function where you can set the lower and upper limit?

0 Kudos
Message 4 of 6
(3,149 Views)

Your statements don't make much sense. It's irrelevant how many NaNs are in the array. A sort will move them all to the bottom. You had said you wanted to find out if all the elements in an array are less than 0.001, and that you've got some NaNs in there. Well, this will do that:

 

 


@twolfe13 wrote:

 I did see how to remove NaN once, but couldn't determine a good way to generalize it other than doing a test loop. I thought there might have been a simple function that I overlooked to do this.


As I noted, there have been several posts in the past about efficient techniques for removing certain elements out of an array. Seek, and ye shall find.

 

 

 

 


@Joseph Loo wrote:

Have you look at the coerce function where you can set the lower and upper limit?


That won't do anything for NaN. Or perhaps I misunderstood what you are suggesting to do?

 

Download All
0 Kudos
Message 5 of 6
(3,144 Views)

Smercurio's solution fails when there are no NaN in the array, since the search function returns -1: you should add a check to handle this case

Message 6 of 6
(966 Views)