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 &];


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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