I am trying to replace Table with more efficient wrapping functions like Map, Apply or their derivatives, like MapThread or @@@. The use of Table is a habit inherited from procedural programming, since it works as DO WHILE loops. I wonder if there is a systematic way to think about wrapping functions when the lists have more than two levels.
For example:
a1 = {{{1, 2, 3, 4}, {5, 6, 7, 8}}, {{9, 10, 11, 12}, {13, 14, 15, 16}}};
a2 = {{17, 18, 19, 20}, {21, 22, 23, 24}};
I want the output produced by
Table[Transpose[{a2[[k]], a1[[q, k]]}], {q, 2}, {k, 2}]
which results in
{{{{17, 1}, {18, 2}, {19, 3}, {20, 4}}, {{21, 5}, {22, 6}, {23,
7}, {24, 8}}}, {{{17, 9}, {18, 10}, {19, 11}, {20, 12}}, {{21,
13}, {22, 14}, {23, 15}, {24, 16}}}}
Can I get the same result without resorting to Table? With a combination of Map or Apply and other functions, perhaps? I have tried many combinations without success.
Thank you in advance