# Removing not-a-number entries from an array of numbers

I have a list of {x, y, z} coordinates from a wavefront sensor I'm working with. However, not all of the imported points have actual values, but they still get imported as {x, y, "NaN"}. I'm trying to find a way to get rid of these faulty points, but so far, I can't find a way to identify them. They don't affect some graphs, but I've run into trouble when performing operations on the set of points

I've tried several things like Delete, DeleteCases and StringTrim, but to no avail.

When I run, DeleteCases[graph, "NaN"], for example, I get back the same set of points.

Also, I am using Mathematica 7.

Thanks.

-
On one of the erroneous data points, would run FullForm and post the result? –  rcollyer Jun 25 '13 at 21:12
I have never seen Mathematica Imports return NaN. This must be a remnant of another program. Anyway, DeleteCases should be able to get rid of them. Please provide the code you tried, otherwise we can't comment on what you did wrong. –  Sjoerd C. de Vries Jun 25 '13 at 21:18
Here is the FullForm for one point:{-1.35714, -1.21429, 0.25 (-0.699 + " NaN")} So far I have tried Delete and DeleteCases, I am new to Mathematica, so I have not explored much else. Thank for your help. –  Brent Jun 25 '13 at 21:54
Try Cases[{{1, 2, 3}, {-1.35714, -1.21429, 0.25 (-0.699 + " NaN")}}, {_, _, _?NumberQ}] or DeleteCases[{{1, 2, 3}, {-1.35714, -1.21429, 0.25 (-0.699 + " NaN")}}, {_, _, _?(Not[NumberQ[#]] &)}] –  Sjoerd C. de Vries Jun 25 '13 at 22:05
Works like a charm! Thank you so much. I will have to familiarize myself with this notation further. –  Brent Jun 25 '13 at 22:12

According to comments, some data points are imported as:

{-1.35714, -1.21429, 0.25 (-0.699 + "NaN")}


where the + indicates that some preprocessing has already been carried out. Per Sjoerd's comment the solutions is to filter cases where each value of a triplet is a valid number:

Cases[{{1, 2, 3}, {-1.35714, -1.21429, 0.25 (-0.699 + " NaN")}}, {_, _, _?NumberQ}]


or remove cases where the triplet's last entry is not a number:

DeleteCases[{{1, 2, 3}, {-1.35714, -1.21429, 0.25 (-0.699 + " NaN")}},
{_, _, _?(Not[NumberQ[#]] &)}]

-
You might want to replace NumberQ with NumericQ, since NumberQ returns false for symbolic numbers, such as Pi. –  The Toad Feb 6 '14 at 23:40

I hope I understand your question correctly. Try this :

x = {{1, 2, 3}, {4, 5, "NaN"}, {6, 7, 7 + 1}}
isOk[u_] := NumberQ[u[[1]]] && NumberQ[u[[2]]] && NumberQ[u[[3]]];
Select[x, isOk[#] &]


output :

{{1, 2, 3}, {6, 7, 8}}


Or even, for higher dimensions :

isOk[u_] := And @@ NumberQ /@u


or (slower but uses less advanced syntax)

isOk[u_] := NumberQ[Total[u]];

-