# Find list position and mapping list

Sorry, my English is not very good.

I have two lists:

tt = {197, 173, 23, 151, 69, 90, 12, 192, 158, 32, 6, 147, 99,
199, 131, 10, 59, 144, 141, 24, 178, 58, 106, 25, 155, 153}

ii = {{46, 154}, {93, 158}, {125, 214}, {160, 112}, {184, 200}, {203,
137}, {230, 266}, {271, 128}, {317, 153}, {362, 251}, {369,
252}, {385, 136}, {424, 195}, {503, 139}, {538, 181}, {582,
268}, {602, 227}, {621, 108}, {660, 147}, {695, 245}, {739,
132}, {766, 242}, {786, 161}, {822, 239}, {857, 128}, {904, 171}}


I can find elements of list tt that are > 150:

Select[tt, # > 150 &]


{197, 173, 151, 192, 158, 199, 178, 155, 153}

Q1 : How can I find the positions of these elements in tt?

I want find answer, such as:

location = {1,2,4,8,9,14,21,25,26}
or location = {1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,1}

How can I mapping to the same location to ii list which get data?

• You want Position and Extract. – Xerxes Feb 21 '13 at 6:55
• @Nasser thanks you for offer the good information !! – Darren Lee Feb 21 '13 at 11:17

Most directly:

Pick[ii, tt, x_ /; x > 150]

{{46, 154}, {93, 158}, {160, 112}, {271, 128}, {317, 153},
{503, 139}, {739, 132}, {857, 128}, {904, 171}}


Or slightly more efficiently:

Pick[ii, Thread[tt > 150]]

{{46, 154}, {93, 158}, {160, 112}, {271, 128}, {317, 153},
{503, 139}, {739, 132}, {857, 128}, {904, 171}}


Some other things to observe:

p1 = Join @@ Position[tt, x_ /; x > 150]

{1, 2, 4, 8, 9, 14, 21, 25, 26}

mask = Boole @ Thread[tt > 150]

{1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1}

ii[[p1]]

{{46, 154}, {93, 158}, {160, 112}, {271, 128}, {317, 153},
{503, 139}, {739, 132}, {857, 128}, {904, 171}}

Pick[ii, mask, 1]

{{46, 154}, {93, 158}, {160, 112}, {271, 128}, {317, 153},
{503, 139}, {739, 132}, {857, 128}, {904, 171}}


Also:

ii ~Extract~ Position[tt, x_ /; x > 150]

{{46, 154}, {93, 158}, {160, 112}, {271, 128}, {317, 153},
{503, 139}, {739, 132}, {857, 128}, {904, 171}}

• Very detailed information, thanks a lot :) – Darren Lee Feb 21 '13 at 11:22
• @user5990 I'm glad you found it useful, and thanks for the Accept. If performance is a prime concern in your application be sure to see the SparseArray method in kguler's post and look at the timings here which relate to a similar problem. – Mr.Wizard Feb 21 '13 at 11:25

Do you want something like this?

Pick[ii, # >= 150 & /@ tt]


=> {{46, 154}, {93, 158}, {160, 112}, {271, 128}, {317, 153}, {503, 139}, {739, 132}, {857, 128}, {904, 171}}

or

location = Boole[# >= 150] & /@ tt


=> {1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1}

and

Pick[ii, location, 1]

• We are thinking along the same lines. (+1) – Mr.Wizard Feb 21 '13 at 8:46
• Thanks for, It the great solution :) – Darren Lee Feb 21 '13 at 11:17
 ttpos=MapIndexed[If[# >= 150, First@#2, ## &[]] &, tt]


or

 ttpos=Select[Range[Length[tt]], tt[[#]] >= 150 &]
(* {1, 2, 4, 8, 9, 14, 21, 25, 26} *)


or

 UnitStep[tt - 150]
(* {1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1}*)

Range[Length[tt]] UnitStep[tt - 150] /. (0) -> Sequence[]


or

 SparseArray[UnitStep[tt - 150]]["AdjacencyLists"]
(* {1, 2, 4, 8, 9, 14, 21, 25, 26} *)


For the corresponding elements in ii:

 ii[[ttpos]]
(* {{46, 154}, {93, 158}, {160, 112}, {271, 128}, {317, 153},
{503,  139}, {739, 132}, {857, 128}, {904, 171}} *)

• I wondered who would post the numeric solution. +1 :-) – Mr.Wizard Feb 21 '13 at 10:44
• @Mr.Wizard, was surprised to see that you did not post your trademark;) – kglr Feb 21 '13 at 11:05
• You mean "NonzeroPositions"/"AdjacencyLists" e.g. (1), (2), (3), (4), (5), (6)? Do I need to hit even a beginner's question with that bludgeon? :^) – Mr.Wizard Feb 21 '13 at 11:15
• I see that while I was finding those links you hit it for me. :o) – Mr.Wizard Feb 21 '13 at 11:16
• @Mr.Wizard, (that too), but I meant ##&[]. – kglr Feb 21 '13 at 11:18

Try like this,

for positions

  positions = Map[
(
If[
(# > 150),
(Position[tt, #]),
("")
]
) &,
tt
];
Cases[Flatten[positions], Except[""]]


for binary values:

  Map[
(
If[
(# > 150),
(1),
(0)
]
) &,
tt
]