7
$\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
  • 7
    $\begingroup$ DeleteCases[_ -> Undefined]@rules? $\endgroup$
    – kglr
    Apr 24, 2022 at 11:54
  • 3
    $\begingroup$ Select[rules, FreeQ[#, Undefined] &] $\endgroup$
    – Bob Hanlon
    Apr 24, 2022 at 15:01

3 Answers 3

8
$\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
    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
    Apr 25, 2022 at 10:19
9
$\begingroup$

Select only NumericQ solution

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

Or delete the instances that include Undefined

rules /.Rule[_,Undefined]->Nothing
$\endgroup$
6
$\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$

Your Answer

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

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