4

The code

With[{matA=Partition[Alphabet[],5],matB=Partition[Range@25,5]},

(* matA and matB are 5x5 matrices*) MapThread[List,{matA,matB},2]

]

is fairly straightforward. The MapThread part does 'interleaving' if matA and matB are thought of as image channels.

How do you do this with plain Thread?

Adam
  • 3,937
  • 6
  • 22
  • 2
    Why do you want to do it with plain Thread? If you already have a solution, can you perhaps explain what problem you are trying to solve that your current solution does not address? – MarcoB Mar 19 '22 at 20:58
  • 1
    I wouldn't call MapThread overpowered per say, but it seems overly generic. I don't need to apply an arbitrary function -- I only want to apply List when I have expressions with other Heads to begin with. – Adam Mar 19 '22 at 21:05

3 Answers3

7

I personally like using the built in restructuring functions for things like this. Things like ArrayReshape, ArrayPad, Riffle, PadLeft/Right, etc. For this particular case, I think you could use Transpose:

Transpose[{Partition[Alphabet[], 5], Partition[Range@25, 5]}, {3, 1, 2}]
lericr
  • 27,668
  • 1
  • 18
  • 64
6
Thread/@Thread[{matA,matB}]==MapThread[List,{matA,matB},2]
user1066
  • 17,923
  • 3
  • 31
  • 49
  • 1
    And, of course, Transpose/@Transpose[{matA,matB}]==MapThread[List,{matA,matB},2]==Transpose/@Thread[{matA,matB}]==Thread/@Transpose[{matA,matB}] – user1066 Mar 20 '22 at 09:35
4

You can also use Flatten:

matC = Flatten[{matA, matB}, {{2}, {3}}]

MatrixForm[matC, TableDirections -> {Column, Row, Row}]

enter image description here

matC == MapThread[List, {matA, matB}, 2]
True
kglr
  • 394,356
  • 18
  • 477
  • 896