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

up vote 4 down vote accepted
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]

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!

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}}

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}

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}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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