4
$\begingroup$

How can I delete the indeterminate from a sample table like the following:

list={{{1,2},3},{{2,2},4},{{1,5},2},{{9,2},9},{{1,10},Indeterminate},{{9,7},2},{{6,8},Indeterminate},{{9,20},3},{{10,2},Indeterminate},{{1,6},6}}?

I tried to use If statement:

Table[If[list[[n, 2]] != Indeterminate, {list[[n, 1]], list[[n, 2]]}]], {n, Length[list]}]

But it can not detect Indeterminate as an element. What can be done?

Note: In particular, I would like to remove the entire element whenever the second entry is Indeterminate, not just the symbol itself.

$\endgroup$
4
  • $\begingroup$ DeleteCases, maybe? $\endgroup$ Commented Apr 2 at 3:56
  • 4
    $\begingroup$ list /. {_, Indeterminate} :> Nothing $\endgroup$ Commented Apr 2 at 5:31
  • $\begingroup$ What is the result come from? Indeterminate is always at the last position? $\endgroup$ Commented Apr 2 at 23:18
  • $\begingroup$ Try list[[n, 2]] =!= Indeterminate. $\endgroup$ Commented Apr 3 at 0:33

3 Answers 3

6
$\begingroup$

Edit

Select[list, Not@*MemberQ[Indeterminate]]

Select[list, FreeQ[Indeterminate]]

{{{1, 2}, 3}, {{2, 2}, 4}, {{1, 5}, 2}, {{9, 2}, 9}, {{9, 7}, 2}, {{9, 20}, 3}, {{1, 6}, 6}}

Original

list /. Indeterminate -> Nothing

or

DeleteCases[list, x_ /; x === Indeterminate, ∞]
$\endgroup$
3
  • $\begingroup$ I wanted to delete the other elements of the group too. $\endgroup$ Commented Apr 2 at 4:34
  • 3
    $\begingroup$ @PKD The updated code is what you want? $\endgroup$ Commented Apr 2 at 4:42
  • 8
    $\begingroup$ I wanted to delete the other elements of the group too. That should be explained in your post. “Delete the indeterminate” is not what you want. $\endgroup$ Commented Apr 2 at 5:04
4
$\begingroup$

On my machine, Pick[list, list[[All, 2]], Except[Indeterminate]] is pretty fast.

Compare by profiling against a longer list:

list = {{{1, 2}, 3}, {{2, 2}, 4}, {{1, 5}, 2}, {{9, 2}, 9}, {{1, 10}, Indeterminate}, 
  {{9, 7}, 2}, {{6, 8}, Indeterminate}, {{9, 20}, 3}, {{10, 2}, Indeterminate}, {{1, 6}, 6}};

Length[list2 = Join @@ ConstantArray[list, 1000]]
10000

Timings of various methods from fastest to slowest, according to my machine:

Pick[list2, list2[[All, 2]], Except[Indeterminate]]; // RepeatedTiming
{0.000349443, Null}
DeleteCases[list2, {_, Indeterminate}]; // RepeatedTiming
{0.000612913, Null}
Extract[list2, Position[list2[[All, 2]], Except[Indeterminate], {1}, Heads -> False]]; // RepeatedTiming
{0.00258426, Null}
list2 /. {_, Indeterminate} :> Nothing; // RepeatedTiming
{0.005552, Null}
Select[list2, Last[#] =!= Indeterminate &]; // RepeatedTiming
{0.00628821, Null}
Select[list2, Not@*MemberQ[Indeterminate]]; // RepeatedTiming
{0.010058, Null}
$\endgroup$
3
$\begingroup$

Does this work?

DeleteCases[list, {_, Indeterminate}]
$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.