Efficient way to multiply a matrix by each element of another matrix and store in one large matrix

What you want is exactly the Kronecker product B \otimes A, computed by the kron function as noted above.

These are arrays of arrays, which is not the same thing as a matrix (2d array) in Julia. (Unlike Python.) You want:

julia> A = [1 2
            3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> B = [11 12
            13 14]
2×2 Matrix{Int64}:
 11  12
 13  14

julia> using LinearAlgebra # for kron

julia> C = kron(B, A)
4×4 Matrix{Int64}:
 11  22  12  24
 33  44  36  48
 13  26  14  28
 39  52  42  56
1 Like