Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}

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

please help me and thank you :)

share|improve this question
You want Position and Extract. – Xerxes Feb 21 at 6:55
see this 12000.org/my_notes/mma_matlab_control/KERNEL/node66.htm it is all about these types of common operations on lists and matrices. see item 12, 18 for your question. – Nasser Feb 21 at 6:59
@Nasser thanks you for offer the good information !! – user5990 Feb 21 at 11:17

4 Answers

up vote 7 down vote accepted

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}}
share|improve this answer
1  
Very detailed information, thanks a lot :) – user5990 Feb 21 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 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]
share|improve this answer
We are thinking along the same lines. (+1) – Mr.Wizard Feb 21 at 8:46
Thanks for, It the great solution :) – user5990 Feb 21 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}} *)
share|improve this answer
I wondered who would post the numeric solution. +1 :-) – Mr.Wizard Feb 21 at 10:44
@Mr.Wizard, was surprised to see that you did not post your trademark;) – kguler Feb 21 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 at 11:15
I see that while I was finding those links you hit it for me. :o) – Mr.Wizard Feb 21 at 11:16
@Mr.Wizard, (that too), but I meant ##&[]. – kguler Feb 21 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
]
share|improve this answer
You can just do Flatten@Position[tt, _?(# >= 150 &)] to get the positions. – Nasser Feb 21 at 7:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.