7
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ On one of the erroneous data points, would run FullForm and post the result? $\endgroup$
    – rcollyer
    Commented Jun 25, 2013 at 21:12
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Jun 25, 2013 at 21:18
  • $\begingroup$ 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. $\endgroup$
    – Brent
    Commented Jun 25, 2013 at 21:54
  • $\begingroup$ 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[#]] &)}] $\endgroup$ Commented Jun 25, 2013 at 22:05
  • $\begingroup$ Works like a charm! Thank you so much. I will have to familiarize myself with this notation further. $\endgroup$
    – Brent
    Commented Jun 25, 2013 at 22:12

2 Answers 2

4
$\begingroup$

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[#]] &)}]
$\endgroup$
1
  • $\begingroup$ You might want to replace NumberQ with NumericQ, since NumberQ returns false for symbolic numbers, such as Pi. $\endgroup$
    – rm -rf
    Commented Feb 6, 2014 at 23:40
3
$\begingroup$

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]];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.