Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
lista = {{0., 1., 0.1, 0.}, {0., 1., 0.2, 0.}, {0., 1., 0.3, 0.}};

listb = {
  {0., 1., 1., 0.}, {0., 0.85, 1., 0.}, {0., 1., 0.1, 0.}, 
  {0.,1., 0.2, 0.}, {0., 1., 0.3, 0.}, {0., 1., 0.4, 0.}, 
  {0., 1., 0.55, 0.}, {0., 1., 0.7, 0.}, {0., 1., 0.85, 0.}, 
  {0.4, 0.4, 0.2, 0.1}, {0.4, 0.4, 0.4, 0.1}, {0.4, 0.4, 0.7, 0.1}, 
  {0.4, 0.7, 0., 0.1}, {0.4, 0.7, 0.1, 0.1}, {0.4, 0.7, 0.2, 0.1}, 
  {0.4, 0.7, 0.4, 0.1}, {0.4, 0.7, 0.7, 0.1}, {0.5, 0.4, 0.4, 0.}, 
  {1., 0., 0., 0.1}, {0., 1., 0., 0.1}, {0., 0., 1., 0.1}, 
  {0.,1., 1., 0.1}, {1., 0., 1., 0.1}, {1., 1., 0., 0.1}};

How would one go about finding the first positions of lista sublists in listb?

share|improve this question
    
@R.M I would presume that's exactly what the OP wants. –  rcollyer Apr 4 '12 at 1:19
    
I've reduced the lists to make this easier to see, and yes I just need the positions of each sublist from lista in listb. –  R Hall Apr 4 '12 at 1:23

2 Answers 2

up vote 8 down vote accepted

How about mapping Position like so

Position[listb, #] & /@ lista
(*{{{3}}, {{4}}, {{5}}, {{6}}, {{7}}, {{8}}, {{9}}, {{1}}, {{2}}}*)

which gives the position of each sublist of lista in listb?

To get only the first instance, use

Position[listb, #, 1, 1] & /@ lista

(the last argument of Position specifies how many positions found to return; the penultimate argument specifies the level at which to search).

share|improve this answer
1  
And with a Flatten you get the simple positions. –  FJRA Apr 4 '12 at 1:22
    
This looks very good, but how can I get the first incidence of lista in listb? IT looks like there are some duplicate sublists in listb. Thanks! –  R Hall Apr 4 '12 at 1:25
1  
@RHall does my edit not select only the first instance? –  acl Apr 4 '12 at 1:53
    
Why yes it does! Thanks very much @acl! –  R Hall Apr 4 '12 at 1:57
Position[listb, Alternatives@@ lista]
share|improve this answer
2  
very nice. isn't Position[listb, Alternatives @@ lista] cleaner? –  acl Apr 4 '12 at 1:33
    
@acl Tnx. For some reason it didn't work the first time I tried. Probably I typed something wrong. –  belisarius Apr 4 '12 at 1:35
    
@belisarius both solutions select multiple incidences of sublists from lista out of listb data. (I made the data set very small for easy reading) but on larger lists duplicate sublists are selected. Thanks for your help! –  R Hall Apr 4 '12 at 1:45
    
@RHall Sorry, I missed the word "first" in your q. –  belisarius Apr 4 '12 at 2:16
    
no @elisarius it is my fault as I edited my post probably right after your first reading. I must be more careful with that in the future. Thanks again for your help! –  R Hall Apr 4 '12 at 9:29

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.