4

In the following example, how to easily get {2,4,10} using the [[{...}]] syntax?

aa = {2, 4, 6, {8, 10, 12}, 14, 16, 18, 20};
aa[[{1, 2}]]
(*{2,4}*)
aa[[{1, 2(* what to put here?? *)}]]
(*{2,4,10}*)
aa[[{1, 2(* what to put here?? *)}]]
(*{2,4,{10,12}}*)
xiaohuamao
  • 4,728
  • 18
  • 36

1 Answers1

1

Try this:

Part[Flatten[aa], #] & /@ {1, 2, 5}

(*  {2, 4, 10}  *)

If you need to first determine the positions of the desired elements do first

pos = Position[Flatten[aa], #] & /@ {2, 4, 10} // Flatten

(* {1, 2, 5}  *)

and then

Part[Flatten[aa], #] & /@ pos

(*  {2, 4, 10}   *)

Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96