9
$\begingroup$

My output from NSolve is a list of rules, most of these are undefined, I want to remove those rules. More specifically I have a list

    rules = {y2 -> Undefined, y3 -> Undefined, y4 -> Undefined, y5 -> Undefined, 
 y6 -> Undefined, y7 -> Undefined, y8 -> Undefined, y9 -> Undefined, 
 y10 -> Undefined, y11 -> Undefined, \[Lambda]2 -> -1.68379, 
 y2 -> 0.991506, y3 -> Undefined, y4 -> Undefined, y5 -> Undefined, 
 y6 -> Undefined, y7 -> Undefined, y8 -> Undefined, y9 -> Undefined, 
 y10 -> Undefined, y11 -> 0.991506, \[Lambda]2 -> Undefined, 
 y2 -> Undefined, y3 -> 0.97847, y4 -> 0.969246, y5 -> 0.963328, 
 y6 -> 0.96037, y7 -> 0.96037, y8 -> 0.963328, y9 -> 0.969246, 
 y10 -> 0.97847, y11 -> Undefined, \[Lambda]2 -> Undefined}

and I want to get

{y2-> 0.991506, y3-> 0.969246,y4 -> 0.969246...}

and so on. How do I remove all the undefined defitions, not that the final list doesnt have to be in this order. DeleteCases and other functions didn't help.

$\endgroup$
2
  • 9
    $\begingroup$ DeleteCases[_ -> Undefined]@rules? $\endgroup$
    – kglr
    Commented Apr 24, 2022 at 11:54
  • 3
    $\begingroup$ Select[rules, FreeQ[#, Undefined] &] $\endgroup$
    – Bob Hanlon
    Commented Apr 24, 2022 at 15:01

5 Answers 5

10
$\begingroup$
t = rules /. HoldPattern[x_ -> Undefined] -> Nothing

EDIT

Another variation:

Values[rules] can be used: (Also try Key[rules] to get variable names)

pos = Position[Values[rules], Except[Undefined], {1}, Heads -> False]

{{11}, {12}, {21}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}}

v = Extract[rules, pos]

result:

{\[Lambda]2->-1.68379,y2->0.991506,y11->0.991506,y3->0.97847,y4->0.969246,y5->0.963328,y6->0.96037,y7->0.96037,y8->0.963328,y9->0.969246,y10->0.97847}

t == v   (* True *)
$\endgroup$
2
  • $\begingroup$ Or rules/.(x_->Undefined):> Nothing (a slight variant of your first method) $\endgroup$
    – user1066
    Commented Apr 25, 2022 at 10:00
  • 1
    $\begingroup$ I have been learning good techniques for the last 24h. Many thanks to everyone who has participated so far. $\endgroup$
    – Syed
    Commented Apr 25, 2022 at 10:19
10
$\begingroup$

Select only NumericQ solution

Select[rules, NumericQ[Last[#]]&]

Or delete the instances that include Undefined

rules /.Rule[_,Undefined]->Nothing
$\endgroup$
8
$\begingroup$

We can use DeleteCases as follows:

f1 = DeleteCases[_ -> Undefined]
(* Alternatively, f1 = DeleteCases[_ ~ _ ~ Undefined];*)

f1 @ rules
{λ2 -> -1.68379, y2 -> 0.991506, y11 -> 0.991506, y3 -> 0.97847 , y4 -> 0.969246,
 y5 -> 0.963328, y6 -> 0.96037, y7 -> 0.96037, y8->0.963328,
 y9 -> 0.969246, y10 -> 0.97847}

Alternatives using operator forms of Select, Cases and ReplaceAll:

f2 = Select @* FreeQ @ Undefined;
f3 = Cases[_~ _ ~ Except @ Undefined];
f4 = ReplaceAll[_ ~ _ ~ Undefined -> Nothing];

f1 @ rules == f2 @ rules == f3 @rules == f4 @ rules
True
$\endgroup$
4
$\begingroup$

Using SequenceSplit (new in 11.3)

Catenate @ SequenceSplit[rules, {x_ /; ! NumericQ @ Last @ x}]

{λ2 -> -1.68379, y2 -> 0.991506, y11 -> 0.991506, y3 -> 0.97847, y4 -> 0.969246, y5 -> 0.963328, y6 -> 0.96037, y7 -> 0.96037, y8 -> 0.963328, y9 -> 0.969246, y10 -> 0.97847}

Using Delete

Delete[rules, Position[rules, _ -> Undefined]]

(* same result *)

Using Extract

Extract[rules, Position[rules, _ -> _?NumericQ]]

(* same result *)

$\endgroup$
3
$\begingroup$

Using Pick and NumericQ:

Pick[#, NumericQ[#[[2]]] & /@ #] &@rules

{λ2 -> -1.68379, y2 -> 0.991506, y11 -> 0.991506, y3 -> 0.97847, y4 -> 0.969246, y5 -> 0.963328, y6 -> 0.96037, y7 -> 0.96037, y8 -> 0.963328, y9 -> 0.969246, y10 -> 0.97847}

$\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.