Skip to main content
15-Moonstone
July 29, 2026
Solved

filterNaN

  • July 29, 2026
  • 2 replies
  • 49 views

Hi There , 

I want to use this function ( filterNAN) , but I’m not sure how it works . I have a nested array f2 . I tried three different approaches but non is working , in f2 I’m using M and sigma they also have NaN in their values , but not all the values are NaN. 

Regards, 

Yusra 

Best answer by LucMeekes

There’s a couple of functions involving NaNs.

First off there is IsNaN(x), which tells you if x is a NaN (or not):

Realise that IsNaN is the only way to test if an item is a NaN. You cannot use comparative expressions to test for NaNs, as shown here:

Defines n to be a NaN
But the comparison of n with NaN results in 0 (False). That is to say, n does not equal NaN, even though it is defined NaN.

Even more clear:

NaN is unequal to NaN. Again: you cannot compare with NaNs.

Then there is markNaN(M,I), wich takes a Matrix and a vector of row/column pairs and marks the items in M indicated by pairs in I as a NaN:

If 

and 

then 

(You can also do this with a vector:

Set the two items of the (first) vector with the indexes of the second vector to NaN.

)

You can find I back from MN with matchNaN(x), which finds the r/c pairs in x that contain a NaN:

(On the vector:

)

Finally there's filterNaN(x), that filters out all rows of x that contain a NaN. So with no NaNs it gives:

But on the modified matrix that contains NaNs it gives:

just the last row of MN, as the first two rows each contain a NaN.

Now note that for an item to be a NaN, it must be a scalar (single value). 

Tells you that the vector [1 NaN] is NOT a NaN (because it is not a scalar)

And so:

filterNaN returns the full vector (of vectors) because it contains no NaN scalars

but

Has filterNaN working with only the first element of the nested vector, that element is a vector and contains a NaN in the second row. Hence it returns a vector with just the first element of it.

Success!
Luc

2 replies

LucMeekes23-Emerald IVAnswer
23-Emerald IV
July 29, 2026

There’s a couple of functions involving NaNs.

First off there is IsNaN(x), which tells you if x is a NaN (or not):

Realise that IsNaN is the only way to test if an item is a NaN. You cannot use comparative expressions to test for NaNs, as shown here:

Defines n to be a NaN
But the comparison of n with NaN results in 0 (False). That is to say, n does not equal NaN, even though it is defined NaN.

Even more clear:

NaN is unequal to NaN. Again: you cannot compare with NaNs.

Then there is markNaN(M,I), wich takes a Matrix and a vector of row/column pairs and marks the items in M indicated by pairs in I as a NaN:

If 

and 

then 

(You can also do this with a vector:

Set the two items of the (first) vector with the indexes of the second vector to NaN.

)

You can find I back from MN with matchNaN(x), which finds the r/c pairs in x that contain a NaN:

(On the vector:

)

Finally there's filterNaN(x), that filters out all rows of x that contain a NaN. So with no NaNs it gives:

But on the modified matrix that contains NaNs it gives:

just the last row of MN, as the first two rows each contain a NaN.

Now note that for an item to be a NaN, it must be a scalar (single value). 

Tells you that the vector [1 NaN] is NOT a NaN (because it is not a scalar)

And so:

filterNaN returns the full vector (of vectors) because it contains no NaN scalars

but

Has filterNaN working with only the first element of the nested vector, that element is a vector and contains a NaN in the second row. Hence it returns a vector with just the first element of it.

Success!
Luc

15-Moonstone
July 30, 2026

thank you so much I appreciate your help