3
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ How about ToExpression[Table [StringJoin["{_,_,#[[",ToString[k],"]]}"],{k,1,n}]] ? You might want to add an & at the end of your list. $\endgroup$
    – Peltio
    Oct 27, 2013 at 21:22
  • $\begingroup$ Can you explain your use case? Are you trying to generate some code? For what end? $\endgroup$
    – Szabolcs
    Oct 27, 2013 at 21:28
  • $\begingroup$ 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 $\endgroup$
    – chipdelmal
    Oct 27, 2013 at 21:30
  • 2
    $\begingroup$ 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. $\endgroup$
    – rm -rf
    Oct 27, 2013 at 22:02
  • $\begingroup$ @rm-rf Agree, but perhaps n != Lenght@tuples[[_]]. It can be fixed easily though $\endgroup$ Oct 27, 2013 at 22:05

2 Answers 2

3
$\begingroup$

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}}  *)
$\endgroup$
3
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Michael E2
    Oct 28, 2013 at 12:58
  • $\begingroup$ @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 $\endgroup$ Oct 28, 2013 at 13:16
  • 1
    $\begingroup$ Could also use data[[All, All, -1]] $\endgroup$ Oct 28, 2013 at 14:12
  • $\begingroup$ @SimonWoods Nice one. Going to add it $\endgroup$ Oct 29, 2013 at 2:06

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.