Between Matrix{Matrix{Any}} and Matrix{Any}

function ⊗(A::T,B::T) where T<:Array
    [i*B for i in A]

a = ⊗(rand(2,2),rand(3,3))
Matrix{Matrix{Float64}} (alias for Array{Array{Float64, 2}, 2})

I know:

a = kron(rand(2,2),rand(3,3))

But I’m trying to get something like:

reduce(hcat,[collect(1:3) for i in 1:3)

which works on a Vector{Vector{Any}} but not on Matrix{Matrix{Any}}.
I tried to play with function cat() and I failed.
I tried cartesian index and I failed.

I mean, it is how our minds treat a matrix right? we can ‘blockrize’ or ‘flatten’ a matrix with very little effort. Can I do that in Julia?

hcat only works for vectors AFAIK. You can flatten a matrix with A[:], then cat it and finally use reshape to get it back into the shape you want. Maybe an easier way is using something like GitHub - JuliaArrays/BlockArrays.jl: BlockArrays for Julia

1 Like

Pardon my English but by “flatten” I mean Matrix{Any}
However I figure something out:

function ⊗(A::T,B::T) where T<:Array
    a = [i*B for i in A] # blocked
    c = reduce(hcat,[vcat(i...) for i in eachcol(a)])# flatten

A = rand(20,20)
B = rand(30,30)
@btime  ⊗(A,B) 
@btime  kron(A,B) 
1 Like