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.

If I have a list of 2D coordinates placed in subarrays of a data structure like the following:

testList = Table[RandomReal[{0, 1000}, {RandomInteger[{0, 32}], 2}], {i, 1, 10}];

And if I sneak in a few entries where 2D coordinates have non-real or non-integer component values (positive or negative Infinity values, etc.) is there a way to quickly prune these coordinates away? In other words, I'd like to go through a data structure like testList and quickly delete any coordinate that has a positive to minus Infinity in it.

Is there a way to make something like the following work?:

Select[testList, {_Real, _Real} &];

What if testList is already flattened by one level? Does the larger context of the data structure matter for Select?

The following works for testList, but is not exactly ideal:

For[i = 1, i <= Length[testList], i++,

  testList[[i]] = 
    Select[testList[[i]], NumberQ[#[[1]]] == True && NumberQ[#[[2]]] == True &];

];
share|improve this question

1 Answer 1

up vote 0 down vote accepted

If the list is flattened, you can do:

 Select[testList, #[[1]] \[Element] Reals && #[[2]] \[Element] Reals &]

If it's not flattened, then you can map the select onto the testList:

Select[#, #[[1]] \[Element] Reals && #[[2]] \[Element] Reals &] & /@ testList

For example, if you have terms like {Infinity,Null}, {2,Null}, or {5,-Infinity} in the testList, these terms are rejected.

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.