11
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ You want Position and Extract. $\endgroup$
    – Xerxes
    Feb 21, 2013 at 6:55
  • $\begingroup$ @Nasser thanks you for offer the good information !! $\endgroup$
    – Darren Lee
    Feb 21, 2013 at 11:17

4 Answers 4

13
$\begingroup$

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}}
$\endgroup$
2
  • 1
    $\begingroup$ Very detailed information, thanks a lot :) $\endgroup$
    – Darren Lee
    Feb 21, 2013 at 11:22
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 11:25
7
$\begingroup$

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]
$\endgroup$
2
  • $\begingroup$ We are thinking along the same lines. (+1) $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 8:46
  • $\begingroup$ Thanks for, It the great solution :) $\endgroup$
    – Darren Lee
    Feb 21, 2013 at 11:17
5
$\begingroup$
 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}} *)
$\endgroup$
9
  • $\begingroup$ I wondered who would post the numeric solution. +1 :-) $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 10:44
  • $\begingroup$ @Mr.Wizard, was surprised to see that you did not post your trademark;) $\endgroup$
    – kglr
    Feb 21, 2013 at 11:05
  • $\begingroup$ 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? :^) $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 11:15
  • $\begingroup$ I see that while I was finding those links you hit it for me. :o) $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 11:16
  • $\begingroup$ @Mr.Wizard, (that too), but I meant ##&[]. $\endgroup$
    – kglr
    Feb 21, 2013 at 11:18
3
$\begingroup$

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
]
$\endgroup$
0

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.