6
$\begingroup$

I have the following list:

l={2 x, x, 4 x, x, x, 2 x, 4 x, x, x, 2 x}

I want to pick those elements that have coefficients bigger than 1, i.e. {2 x, 4 x, 2 x, 4 x, 2 x} I tried

Select[CoefficientList[{2 x, x, 4 x, x, x, 2 x, 4 x, x, x, 2 x}, 
    x][[All, 2]], # > 1 &]*x

which return what I need but I believe there must be a much shorter and faster way? Also I would like to find the position of {2 x, 4 x, 2 x, 4 x, 2 x} in my original list so I did:

Position[{2 x, x, 4 x, x, x, 2 x, 4 x, x, x, 2 x}, {2 x, 4 x, 2 x, 
  4 x, 2 x}]

which does not work so I must be wrong

$\endgroup$
4
$\begingroup$
l = {2 x, x, 4 x, x, x, 2 x, 4 x, x, x, 2 x};

s = Select[CoefficientList[l, x][[All, 2]], # > 1 &]*x

p = Position[l, Alternatives @@ Union[s]]

{{1}, {3}, {6}, {7}, {10}}

Extract[l, p]
$\endgroup$
5
$\begingroup$

You might do like this:

l /. x -> 1 /. x_ /; x == 1 -> Nothing /. a_ -> a*x
(*   {2 x, 4 x, 2 x, 4 x, 2 x}   *)

Have fun!

$\endgroup$
5
$\begingroup$

You can try

Pick[l,Exponent[x,l],0]   

{2 x, 4 x, 2 x, 4 x, 2 x}

or

Flatten[Take[l,#]&/@Position[Exponent[x,l],0]]     

Also if you want the positions try

Position[Exponent[x, l], 0]   

{{1}, {3}, {6}, {7}, {10}}

$\endgroup$
3
$\begingroup$
l={2 x, x, 4 x, x, x, 2 x, 4 x, x, x, 2 x};
Pick[l, Unitize[D[l, x] - 1], 1]

{2 x, 4 x, 2 x, 4 x, 2 x}

  Flatten@Position[Unitize[D[l, x] - 1], 1]

{1, 3, 6, 7, 10}

$\endgroup$
3
$\begingroup$
l = {2 x, x, 4 x, -x, x, 2 x, 4 x, x, x, 2 x};
test = Clip[Coefficient[l, x], {1, 1}, {1, 0}]; (* 1 means coeffient <= 1, else 0 *)
l2 = Pick[l, test, 0]
pos = Ordering[test, Length[l2]]

{2 x, 4 x, 2 x, 4 x, 2 x}
{1, 3, 6, 7, 10}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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