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.

I need to create a list that as an input receives the number $N$ of elements that it will contain and that returns:

{{_,_,#[[1]]},{_,_,#[[2]]},{_,_,#[[3]]},...{_,_,#[[N]]}}

I've tried using Hold and Release but haven't come up with a solution.

share|improve this question
    
How about ToExpression[Table [StringJoin["{_,_,#[[",ToString[k],"]]}"],{k,1,n}]] ? You might want to add an & at the end of your list. –  Peltio Oct 27 '13 at 21:22
    
Can you explain your use case? Are you trying to generate some code? For what end? –  Szabolcs Oct 27 '13 at 21:28
    
I am trying to generate this list to use it in a pattern match operation: Position[ToExpression /@ indParList, {{, _, #[[1]]}, {, _, #[[2]]}}] & /@ tuples in which the tuples list may vary in length and therefore the pattern match array should adapt to this variation –  chipdelmal Oct 27 '13 at 21:30
2  
This is a bad way of doing it. Why not just transpose your data and then use the last element? It will automatically work for all values of n. –  rm -rf Oct 27 '13 at 22:02
    
@rm-rf Agree, but perhaps n != Lenght@tuples[[_]]. It can be fixed easily though –  belisarius Oct 27 '13 at 22:05

2 Answers 2

up vote 3 down vote accepted

Perhaps Thread[{_, _, #}] applied to each tuple thus:

SeedRandom[1];
data = Table[RandomInteger[1, {RandomInteger[{2, 4}], 3}], {100}];
tuples = Table[RandomInteger[1, RandomInteger[{2, 4}]], {5}];

Application:

pos = Position[data, Thread[{_, _, #}]] & /@ tuples
(* {{{38}, {82}}, {{33}, {43}, {47}, {94}}, {{58}}, {{37}, {85}, {88}}, {}} *)

Check:

Grid[
 MapThread[{Map[Last, Extract[data, #1], {2}], #2} &, {pos, tuples}],
 Alignment -> Left]
(* {{1, 1, 0, 0}, {1, 1, 0, 0}}                   {1, 1, 0, 0}
   {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}}   {1, 1, 1}
   {{0, 0, 1, 0}}                                 {0, 0, 1, 0}
   {{1, 0, 0}, {1, 0, 0}, {1, 0, 0}}              {1, 0, 0}
   {}                                             {0, 0, 1, 1}}  *)
share|improve this answer

Perhaps you could also consider:

(* Michael's data *)
SeedRandom[1];
data = Table[RandomInteger[1, {RandomInteger[{2, 4}], 3}], {100}];
tuples = Table[RandomInteger[1, RandomInteger[{2, 4}]], {5}];

Then:

Position[Last /@ Transpose /@ data, Alternatives @@ tuples]
(*
==> {{33}, {37}, {38}, {43}, {47}, {58}, {82}, {85}, {88}, {94}}
*)

or

Position[Last /@ Transpose /@ data, #] & /@ tuples
(*
==> {{{38}, {82}}, {{33}, {43}, {47}, {94}}, {{58}}, {{37}, {85}, {88}}, {}}
*)

or Simon's (slickest)

Position[data[[All, All, -1]], #] & /@ tuples
share|improve this answer
    
I was waiting for rm-rf or you to post this. The Alternatives one will probably bog down on long lists of tuples. But the second one should be faster than mine. –  Michael E2 Oct 28 '13 at 12:58
    
@MichaelE2 I've seen his comment a few hours ago, but thought he wasn't going to post it. Anyway this is the first thing I would think of for this problem, and surely not pattern matching –  belisarius Oct 28 '13 at 13:16
1  
Could also use data[[All, All, -1]] –  Simon Woods Oct 28 '13 at 14:12
    
@SimonWoods Nice one. Going to add it –  belisarius Oct 29 '13 at 2:06

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.