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.

I have the following list:

 m = {{x == 0, y == 0.29264681456942615}, 
      {x == 30, y == 0.2419119568894183}, 
      {x == 50, y == 0.1485164898707659}, 
      {x == 70, y == 0.05437093382683481}, 
      {x == 90 , y == 1.}}

I would like to convert it to the form {{0,0.29264681456942615},{30, 0.2419119568894183}, ... } (extracting only the numbers form the list). How can I do this?

share|improve this question
    
m[[;; , ;; , 2]] should do the trick. To see why check the result of the following: Head[a==b], (a==b)[[0]], (a==b)[[1]], (a==b)[[2]] –  ssch Nov 6 '12 at 14:32
4  
alternatively m /. Equal[_, b_] -> b –  chris Nov 6 '12 at 14:42
    
That's way neater since it can be used even if the order was {3==x,8==y} in some places, m /. Equal[a_, b_] :> If[NumberQ[a], a, b] –  ssch Nov 6 '12 at 14:54
2  
Map[Last, m, {2}]. –  J. M. Nov 6 '12 at 14:56
2  
@ssch equivalent but perhaps less cryptic for the novice: m[[All,All,2]] –  Yves Klett Nov 6 '12 at 15:05
show 2 more comments

3 Answers 3

One of many possible ways is

{x, y} /. (m /. Equal -> Rule)

What happens here: Let's say you want to transform the expression x equals 0 into x is replaced by 0, then you can do exactly this by ReplaceAll (ok, replacing all) Equals in your list into Rules which is done by (m /. Equal -> Rule). The rest is to use it and replace {x,y} with these rules.

One disadvantage of this approach which is shared by following solutions from the comments:

m[[;; , ;; , 2]]
m[[All,All,2]]
m[[All, All, -1]]
m /. Equal[_, b_] -> b
Map[Last, m, {2}]

is that it relies on the fact, that your number is in the end. This can be prevented by using for instance a rule which checks where the numeric value is like suggested by ssch

m /. Equal[a_, b_] :> If[NumberQ[a], a, b]
share|improve this answer
    
Hmmm... why the RuleDelayed in your first example? Rule works too. +1 for the didactic explanation. –  István Zachar Nov 6 '12 at 15:18
    
@IstvánZachar Thx. No special reason. I use RuleDelayed more often, since in most of my applications I don't want to rhs to be evaluated. But I see, it is maybe confusing the OP here. –  halirutan Nov 6 '12 at 15:20
    
@halirutan the last rule still needs to be delayed (I couldn't edit because 1 character was considered a low quality edit by the system) –  ssch Nov 6 '12 at 15:31
    
@ssch Ah, yes, sorry. I was a bit beside me. –  halirutan Nov 6 '12 at 15:37
    
@ssch To explain why I thought it was not necessary: something like If[boing, a, b] stays unevaluated as long as boing is not True or False. I thought we can use this here and forgot that NumberQ[a] is instantly evaluated to False. Sorry again. –  halirutan Nov 6 '12 at 15:40
add comment
m = {{x == 0, y == 0.29264681456942615}, {x == 30, 
y == 0.2419119568894183}, {x == 50, 
y == 0.1485164898707659}, {x == 70, 
y == 0.05437093382683481}, {x == 90, y == 1.}};

m /. {x == a_, y == b_} -> {a, b}

This returns

{{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90, 1.}}
share|improve this answer
add comment

m = {{x == 0, y == 0.29264681456942615}, {x == 30, y == 0.2419119568894183}, {x == 50, y == 0.1485164898707659}, {x == 70, y == 0.05437093382683481}, {x == 90, y == 1.}};

If lists had this shape I would just

m[[All, All, 2]]

Alternatives are

m /. _ == n_ :> n

or

{x, y} /. (m /. Equal -> Rule)

or even

Block[{Equal = CompoundExpression}, m]

or odd

{x, y} /. First /@ Solve /@ m

all to get

(* {{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90, 
  1.}} *)
share|improve this answer
    
Ok, I just saw that all these except the even and odd ones were already mentioned in comments or answers –  Rojo Nov 7 '12 at 0:08
add comment

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.