4
$\begingroup$

I have a variable size array of lists and would like to set up a MapThread of the lists with a variable pure function $\{\#1,\#2,...,\#n\}\&$ with $n$ being the number of rows in the array and am unable to code this as if I try constructs like

function = Table[
  "#" <> ToString[n],
  {n, 1, 5}]

(* {"#1", "#2", "#3", "#4", "#5"} *)

or:

function = Table[
  ToString[#] <> ToString[n],
  {n, 1, 5}]

(*  {"#11", "#12", "#13", "#14", "#15"} *)

and then MapThread the function like:

MapThread[
 function &, {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2,3}}]

I receive the error

incompatible dimensions of objects at positions {2, 4} and {2, 5} of
MapThread[function&,{{1,2,3},{4,5,6},{1,2,3},{4,5,6},{1,2.3}}];
dimensions are 3 and 2

Can someone help me with this or perhaps refer a link that would demonstrate this type of construct?

Thanks.

$\endgroup$
2
  • $\begingroup$ Adding a bounty suggest that none of the answers so far were sufficient. But they all seem to solve the problem as presented. Is there more information you can provide to indicate why these answers don't suffice? $\endgroup$
    – lericr
    Feb 20 at 17:57
  • $\begingroup$ They are all sufficient. I just want to reward an existing answer as per the bounty description. Also trivial looking, it was a bottleneck in my work and the solutions helped my progress and I appreciate the expertise given. $\endgroup$
    – josh
    Feb 20 at 18:09

4 Answers 4

3
+50
$\begingroup$

I'm curious about why you think you need to specify all the slots. MapThread works just fine like this:

list = {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}};

MapThread[g, list]
(* {g[1, 4, 1, 4, 1], g[2, 5, 2, 5, 2], g[3, 6, 3, 6, 3]} *)

And in your specific example, we can just swap g for List:

MapThread[List, list]
(* {{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}} *)

And if you do need slots for some reason, you don't need to specify all of them. You can just use SlotSequence:

MapThread[h[##] &, list]
(* {h[1, 4, 1, 4, 1], h[2, 5, 2, 5, 2], h[3, 6, 3, 6, 3]} *)
$\endgroup$
1
  • $\begingroup$ Thanks for that. In an earlier comment which I deleted, I thought List wouldn't work but was only due to my lack of understanding. The construct MapThread[List,myList] is really concise and reduces the amount of code.. $\endgroup$
    – josh
    Feb 19 at 14:24
4
$\begingroup$

No need to make list of each slot, use ## which is sequence of all slots/arguments.

MapThread[{##} &, {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 
   3}}]

{{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}}

The same can be achieved by:

Transpose@{{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}}
$\endgroup$
0
2
$\begingroup$
list = {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}};

fun = Table[Slot @ n, {n, 5}];

MapThread[Evaluate @ fun &, list]

{{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}}

The Evaluate is needed because Table has the attribute HoldAll

$\endgroup$
0
2
$\begingroup$
list = {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}};

Another way using Thread and MapApply:

Thread[list]

(*{{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}}*)

g @@@ Thread[list]

(*{g[1, 4, 1, 4, 1], g[2, 5, 2, 5, 2], g[3, 6, 3, 6, 3]}*)
$\endgroup$

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.