5
$\begingroup$

I realise that

Position[{2, 4, 8, 16, 32, 64}, 4]

will return

{{2}}

I would like to do something like this:

Position[{2, 4, 8, 16, 32, 64}, {4, 32, 64}]

and get

{{2}, {5}, {6}}

I have searched, but can't ind anything - I am clearly missing something obvious :/

$\endgroup$
13
$\begingroup$

Position takes a pattern as last argument. Therefore, the most direct way is

Position[{2, 4, 8, 16, 32, 64}, 4 | 32 | 64]

or

Position[{2, 4, 8, 16, 32, 64}, Alternatives @@ {4, 32, 64}]

if you have your numbers in a list.

$\endgroup$
  • $\begingroup$ Great - the latter is more useful for me - very simple question, I know - but hugely time saving! Many thanks :) $\endgroup$ – martin Dec 30 '13 at 21:09
4
$\begingroup$

If you want them in the order listed there is the direct approach.

 Flatten[Position[{2, 4, 8, 16, 32, 64}, #], 1] & /@ {32, 64,4}

     ->     { {5}, {6}, {2} }
$\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.