Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list of pairs of coordinates, where some are missing.

coordinates = {
  {{1, 1}, {1, 2}},
  {{3, 4}, {5, 5}},
  {{1, 2}, Missing[]},
  {{9, 8}, {7, 7}},
  {Missing[], {2, 3}}
}

I am trying to remove the pairs containing missing coordinates, to get

coordinates = {
  {{1, 1}, {1, 2}},
  {{3, 4}, {5, 5}},
  {{9, 8}, {7, 7}},
}

I tried

coordinates = DeleteCases[coordinates, MemberQ[#, _Missing] &]

but that did not work. But the function MemberQ works on pairs directly:

MemberQ[coordinates[[1]], _Missing] (* returns False *)
MemberQ[coordinates[[3]], _Missing] (* returns True *)

What did I do wrong?

share|improve this question
1  
DeleteCases[coordinates, _?(MemberQ[#, _Missing] &)] or DeleteCases[coordinates, x_ /; MemberQ[x, _Missing]] work –  andre Jun 6 at 17:32
    
Related: (1234), (2655), (29159) –  Mr.Wizard Jun 7 at 14:21

3 Answers 3

up vote 3 down vote accepted
coordinates = {{{1, 1}, {1, 2}}, {{3, 4}, {5, 5}}, {{1, 2}, Missing[]}, {{9, 8}, {7, 7}}, {Missing[], {2, 3}}};

The second argument to DeleteCases must be a pattern not a boolean.

correctedCoordinates = DeleteCases[coordinates, _?(MemberQ[#, _Missing] &)]

{{{1, 1}, {1, 2}}, {{3, 4}, {5, 5}}, {{9, 8}, {7, 7}}}

Similarly with Cases

correctedCoordinates == Cases[coordinates, _?(FreeQ[#, _Missing] &)]

True

Whereas, Select requires a boolean.

correctedCoordinates == Select[coordinates, FreeQ[#, _Missing] &]

True

share|improve this answer
    
Yes, you're right. I was mistaken about what was going wrong. Do you know what happened to MathGroup? No new messages since May 22. –  Szabolcs Jun 6 at 18:31
    
Presumably, most/all of the traffic migrated here and the moderator fell asleep. :-) –  Bob Hanlon Jun 6 at 18:54
DeleteCases[coordinates, {___, Missing[], ___}]

Or (slower)

Cases[coordinates, {{__?NumberQ}, {__?NumberQ}}]

I like this style because of its flexibility in more complicated cases.

With

x = Join @@ Table[coordinates, {10^5}];

Eldo DeleteCases : 0.22 sec.
Szabolcs: : 0.86 sec.
Bob Hanlon DeleteCases : 0.95 sec.

share|improve this answer

Bob Hanlon has the answer, but merely as a supplement: if your data is as you show I would simply use Select and MatrixQ.

Select[coordinates, MatrixQ]
{{{1, 1}, {1, 2}}, {{3, 4}, {5, 5}}, {{9, 8}, {7, 7}}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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