11

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?

LCarvalho
  • 9,233
  • 4
  • 40
  • 96
Darren Lee
  • 587
  • 4
  • 11

4 Answers4

13

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}}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    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
7

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]
user1066
  • 17,923
  • 3
  • 31
  • 49
5
 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}} *)
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
kglr
  • 394,356
  • 18
  • 477
  • 896
3

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
]
subbu
  • 2,304
  • 1
  • 13
  • 32