# How to extract elements from a Table

I was trying to find the maximum of a function and where it maximises. I found that FindMaximum will give the result. Now I need to save the maximum value and x value(using replace rule) in two different variables.

I have a Table like this:

T1 = {  0.686151, x -> 1.54793, 0.686075, x -> 1.55549, 0.687346,
x -> 1.54975, 0.689603, x -> 1.52983, 0.692579,
x -> 1.49448, 0.696075, x -> 1.44192, 0.699942,
x -> 1.36952, 0.704069, x -> 1.27328, 0.708369,
x -> 1.14652, 0.712778, x -> 0.976704, 0.717245,
x -> 0.733626, 0.721731, x -> 0.256813, 0.726028,
x -> 1.84992*10^-9, 0.729878, x -> 1.01046*10^-9, 0.733336,
x -> 4.23067*10^-7, 0.736454, x -> 1.1326*10^-8, 0.739275,
x -> 5.37651*10^-10, 0.741835, x -> 9.37745*10^-6, 0.744167,
x -> 3.23782*10^-6, 0.746296, x -> 1.28956*10^-6, 0.748245,
x -> 5.7631*10^-7, 0.750034, x -> 2.82576*10^-7, 0.75168,
x -> 1.49276*10^-7, 0.753197, x -> 8.37318*10^-8, 0.754599,
x -> 4.92927*10^-8, 0.755898, x -> 3.01754*10^-8, 0.757103,
x -> 1.90688*10^-8, 0.758222, x -> 1.23672*10^-8, 0.759265,
x -> 8.19418*10^-9, 0.760237, x -> 5.52643*10^-9, 0.761145,
x -> 3.78294*10^-9, 0.761994, x -> 2.62213*10^-9, 0.76279,
x -> 1.83701*10^-9, 0.763537, x -> 1.29881*10^-9, 0.764238,
x -> 9.25594*10^-10, 0.764898, x -> 6.64199*10^-10};


and I need this:

T2 =  {{0.686151}, {0.686075}, {0.687346}, {0.689603}, {0.692579}, \
{0.696075}, {0.699942}, {0.704069}, {0.708369}, {0.712778}, \
{0.717245}, {0.721731}, {0.726028}, {0.729878}, {0.733336}, \
{0.736454}, {0.739275}, {0.741835}, {0.744167}, {0.746296}, \
{0.748245}, {0.750034}, {0.75168}, {0.753197}, {0.754599}, \
{0.755898}, {0.757103}, {0.758222}, {0.759265}, {0.760237}, \
{0.761145}, {0.761994}, {0.76279}, {0.763537}, {0.764238}, {0.764898}};

T3 = {  x -> 1.54793, x -> 1.55549,
x -> 1.54975, x -> 1.52983,
x -> 1.49448,x -> 1.44192,
x -> 1.36952, x -> 1.27328,
x -> 1.14652, x -> 0.976704,
x -> 0.733626, x -> 0.256813,
x -> 1.84992*10^-9, x -> 1.01046*10^-9,
x -> 4.23067*10^-7, x -> 1.1326*10^-8,
x -> 5.37651*10^-10,  x -> 9.37745*10^-6, 0.744167,
x -> 3.23782*10^-6, 0.746296, x -> 1.28956*10^-6,
x -> 5.7631*10^-7,  x -> 2.82576*10^-7
x -> 1.49276*10^-7,  x -> 8.37318*10^-8,
x -> 4.92927*10^-8,  x -> 3.01754*10^-8,
x -> 1.90688*10^-8, x -> 1.23672*10^-8,
x -> 8.19418*10^-9,  x -> 5.52643*10^-9,
x -> 3.78294*10^-9,  x -> 2.62213*10^-9,
x -> 1.83701*10^-9,  x -> 1.29881*10^-9,
x -> 9.25594*10^-10,  x -> 6.64199*10^-10};


• Maybe T1[[1;;-1;;2]] for T2 and T1[[2;;-1;;2]] for T3... Have a look at Part and Span. – Henrik Schumacher Feb 13 '18 at 9:03

T1 = {0.686151,x->1.54793,0.686075,x->1.55549,0.687346,x->1.54975,0.689603,x->1.52983,0.692579,x->1.49448,0.696075,x->1.44192,0.699942,x->1.36952,0.704069,x->1.27328,0.708369,x->1.14652,0.712778,x->0.976704,0.717245,x->0.733626,0.721731,x->0.256813,0.726028,x->1.84992*10^-9,0.729878,x->1.01046*10^-9,0.733336,x->4.23067*10^-7,0.736454,x->1.1326*10^-8,0.739275,x->5.37651*10^-10,0.741835,x->9.37745*10^-6,0.744167,x->3.23782*10^-6,0.746296,x->1.28956*10^-6,0.748245,x->5.7631*10^-7,0.750034,x->2.82576*10^-7,0.75168,x->1.49276*10^-7,0.753197,x->8.37318*10^-8,0.754599,x->4.92927*10^-8,0.755898,x->3.01754*10^-8,0.757103,x->1.90688*10^-8,0.758222,x->1.23672*10^-8,0.759265,x->8.19418*10^-9,0.760237,x->5.52643*10^-9,0.761145,x->3.78294*10^-9,0.761994,x->2.62213*10^-9,0.76279,x->1.83701*10^-9,0.763537,x->1.29881*10^-9,0.764238,x->9.25594*10^-10,0.764898,x->6.64199*10^-10};


Then

{T2, T3} = GatherBy[T1, Head]


Or alternatively

pos = Drop[Position[T1, Rule, {2}], 0, -1];
T2 = Delete[T1, pos];
T3 = Extract[T1, pos];

• Thank you Sir.. Thanks a lot – Hari Krishnan Feb 13 '18 at 9:44
• nice use of Head in GatherBy – Mike Honeychurch Feb 14 '18 at 0:46
{T2a, T3} = T1[[# ;; ;; 2]] & /@ {1, 2};


or

{T2b, T3b} = Pick[T1, Head /@ T1, #] & /@ {Real, Rule}

{T2a == T2b, T3 == T3b}


{True, True}

T2 = List/ @ T2a


{{0.686151},{0.686075},{0.687346},{0.689603},{0.692579},{0.696075},{0.699942},{0.704069},{0.708369},{0.712778},{0.717245},{0.721731},{0.726028},{0.729878},{0.733336},{0.736454},{0.739275},{0.741835},{0.744167},{0.746296},{0.748245},{0.750034},{0.75168},{0.753197},{0.754599},{0.755898},{0.757103},{0.758222},{0.759265},{0.760237},{0.761145},{0.761994},{0.76279},{0.763537},{0.764238},{0.764898}}

T3


{x->1.54793,x->1.55549,x->1.54975,x->1.52983,x->1.49448,x->1.44192,x->1.36952,x->1.27328,x->1.14652,x->0.976704,x->0.733626,x->0.256813,x->1.84992*10^-9,x->1.01046*10^-9,x->4.23067*10^-7,x->1.1326*10^-8,x->5.37651*10^-10,x->9.37745*10^-6,x->3.23782*10^-6,x->1.28956*10^-6,x->5.7631*10^-7,x->2.82576*10^-7,x->1.49276*10^-7,x->8.37318*10^-8,x->4.92927*10^-8,x->3.01754*10^-8,x->1.90688*10^-8,x->1.23672*10^-8,x->8.19418*10^-9,x->5.52643*10^-9,x->3.78294*10^-9,x->2.62213*10^-9,x->1.83701*10^-9,x->1.29881*10^-9,x->9.25594*10^-10,x->6.64199*10^-10}

• Thank you alllll – Hari Krishnan Feb 14 '18 at 10:08

Since you seem to be separating based on Rule:

Cases[t1, x : Except[_Rule] :> {x}]
Cases[t1, _Rule]

(*
{{0.686151}, {0.686075}, {0.687346}, {0.689603}, {0.692579},
{0.696075}, {0.699942}, {0.704069}, {0.708369}, {0.712778},
{0.717245}, {0.721731}, {0.726028}, {0.729878}, {0.733336},
{0.736454}, {0.739275}, {0.741835}, {0.744167}, {0.746296},
{0.748245}, {0.750034}, {0.75168}, {0.753197}, {0.754599},
{0.755898}, {0.757103}, {0.758222}, {0.759265}, {0.760237},
{0.761145}, {0.761994}, {0.76279}, {0.763537}, {0.764238}, {0.764898}}
*)

(*

{x -> 1.54793, x -> 1.55549, x -> 1.54975, x -> 1.52983, x -> 1.49448,
x -> 1.44192, x -> 1.36952, x -> 1.27328, x -> 1.14652,
x -> 0.976704, x -> 0.733626, x -> 0.256813, x -> 1.84992*10^-9,
x -> 1.01046*10^-9, x -> 4.23067*10^-7, x -> 1.1326*10^-8,
x -> 5.37651*10^-10, x -> 9.37745*10^-6, x -> 3.23782*10^-6,
x -> 1.28956*10^-6, x -> 5.7631*10^-7, x -> 2.82576*10^-7,
x -> 1.49276*10^-7, x -> 8.37318*10^-8, x -> 4.92927*10^-8,
x -> 3.01754*10^-8, x -> 1.90688*10^-8, x -> 1.23672*10^-8,
x -> 8.19418*10^-9, x -> 5.52643*10^-9, x -> 3.78294*10^-9,
x -> 2.62213*10^-9, x -> 1.83701*10^-9, x -> 1.29881*10^-9,
x -> 9.25594*10^-10, x -> 6.64199*10^-10}

*)