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

**URL:** https://discourse.julialang.org/t/between-matrix-matrix-any-and-matrix-any/74534
**Category:** General Usage
**Tags:** question, arrays
**Created:** [January 13, 2022, 8:17am UTC](https://discourse.julialang.org/t/between-matrix-matrix-any-and-matrix-any/74534 "2022-01-13T08:17:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![frydaydeep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frydaydeep/32/32078_2.png) [@frydaydeep](https://discourse.julialang.org/u/frydaydeep)
#### Post date: [January 13, 2022, 8:17am UTC](https://discourse.julialang.org/t/between-matrix-matrix-any-and-matrix-any/74534/1 "2022-01-13T08:17:46Z")

</div>

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

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

```

I know:

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

```

But I’m trying to get something like:

```julia
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?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 13, 2022, 8:22am UTC](https://discourse.julialang.org/t/between-matrix-matrix-any-and-matrix-any/74534/2 "2022-01-13T08:22:02Z")

</div>

`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 [https://github.com/JuliaArrays/BlockArrays.jl](https://github.com/JuliaArrays/BlockArrays.jl)

---

<div class="post-metadata">

### Author: ![frydaydeep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frydaydeep/32/32078_2.png) [@frydaydeep](https://discourse.julialang.org/u/frydaydeep)
#### Post date: [January 13, 2022, 12:20pm UTC](https://discourse.julialang.org/t/between-matrix-matrix-any-and-matrix-any/74534/3 "2022-01-13T12:20:21Z")

</div>

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

```julia
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,c
end

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

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 2, 2022, 7:14am UTC](https://discourse.julialang.org/t/between-matrix-matrix-any-and-matrix-any/74534/4 "2022-11-02T07:14:01Z")

</div>

I don’t think it’s very efficient, but just to show Julia’s many possibilities …

```julia
hvcat(size(A),.*(A, [B])...)

```

```julia
reshape(vcat(.*(A, [B])...),size(A).*size(B))

reshape(reduce(vcat, A.*[B]), size(A).*size(B))

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 2, 2022, 9:28am UTC](https://discourse.julialang.org/t/between-matrix-matrix-any-and-matrix-any/74534/5 "2022-11-02T09:28:22Z")

</div>

I can’t recommend the package `TensorCast.jl` highly enough for this kind of array index wrangling:

```julia
A, B = rand(20,20), rand(30,30)

using TensorCast
@cast C[m⊗i,n⊗j] := A[i,j] * B[m,n]

C == kron(A,B) # true

```

In this case there is a built-in `kron` function available, but in most cases it is more complicated for regular users to find an effective solution.
