1
$\begingroup$

If have the following command

Position[{a, b, a, a, b, c, b}, b]

(* Out[1]= {{2}, {5}, {7}} *)

Is it possible to get only a sequence of values instead of this list, like

Out[2]= 2, 5, 7

I'm interested in the values only and don't need the list structure.

$\endgroup$
4
  • $\begingroup$ Have a look at Flatten. $\endgroup$ Commented Mar 24, 2013 at 10:51
  • $\begingroup$ Out[2]=2,5,7 is not mathematica syntax. Do you mean Out[2]={2,5,7} ? $\endgroup$
    – andre314
    Commented Mar 24, 2013 at 11:17
  • $\begingroup$ Out[2]={2,5,7} is possible? how to do it @andre? If the output become 2, 5 , 7, how to manipulate it? so it come to be Out[2]=2,5,7? thanks $\endgroup$
    – user6527
    Commented Mar 24, 2013 at 11:42
  • $\begingroup$ Out[2]={2,5,7} is possible and trivial. To do it use Flatten, as suggested by @b.gatessucks. The question "How to manipulate it" as so many possible interpretations that I can't answer. Otherwise I don't understand : "so it come to be ..." $\endgroup$
    – andre314
    Commented Mar 24, 2013 at 11:58

1 Answer 1

11
$\begingroup$

The question is, what do you want to do with the output. The output of Position is in a form so that it can directly be used with Extract

list = {a, b, a, a, b, c, b};
pos = Position[list, b];
Extract[list, pos]
(* {b, b, b} *)

For this simple example, it is a bit useless because we already know, that on all positions pos we have a b in list. Unfortunately, your simple example covers another thing, which is that you really need the nested structure {{2}, {5}, {7}} in non-trivial cases.

If you really just want the values, than I suggest (as the others too) to use Flatten

Flatten[pos]
(* {2, 5, 7} *)

When your input is really just a one-dimensional structure or list, the above can be used with Part to extract the values of list

Part[list, Flatten[pos]]

(* or a bit shorter *)

list[[Flatten[pos]]]

In most cases you cannot simply flatten the output and to give you a small non-trivial example let's assume we have a somewhat moderate complex expression

 expr = 20 x^3 Cos[x + x^5] - Sin[x + x^5] - 10 x^4 Sin[x + x^5] - 25 x^8 Sin[x + x^5]

Now you want to use Position to find all positions where x appears with an exponent

pos = Position[expr, x^_]
(* {{1, 2}, {1, 3, 1, 2}, {2, 2, 1, 2}, {3, 2}, {3, 3, 1, 2}, {4, 2}, {4, 3, 1, 2}} *)

What you see is, that the structure is nested and this has a reason: To find the first x^_ which is at position {1,2} you have to select the first part which is

20 x^3 Cos[x + x^5]

and there the second one. Even more nested it gets, when the search pattern in located inside one of the Sin's. If you flatten this output of Position you get nothing useful but feeding the output to Extract gives you everything you wanted

Extract[expr, pos]

(* {x^3, x^5, x^5, x^4, x^5, x^8, x^5} *)
$\endgroup$
2
  • $\begingroup$ For the case of a vector, perhaps it is worth adding that his desired output can be used nicely with Part. +1 $\endgroup$
    – Rojo
    Commented Mar 24, 2013 at 14:23
  • $\begingroup$ @Rojo Thanks and your suggestion is now included. $\endgroup$
    – halirutan
    Commented Mar 24, 2013 at 15:00

Your Answer

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

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