Before I begin, I hope MathJax works on Discourse.

I am trying to do a generalization of Schmidt decomposition on a matrix, which means writing down a matrix H of size m\times n, where m = m_1 \times m_2 and n = n_1\times n_2, as

using the minimum number of terms possible. Here, H_{1,j} and H_{2,j} are matrices of sizes m_1 \times n_1 and m_2 \times n_2 respectively and \otimes is a Kronecker product. What I was trying to do is similar to what is explained in this answer, with a tiny difference at the end.

There are three main steps in this algorithm:

- Rearranging elements of H into a matrix of size m_1 n_1 \times m_2 n_2
- Doing a Singular Value Decomposition on the result from previous step
- Rearranging the outcome of SVD into the form we wanted

The second and third step are straightforward in Julia. For the first step what I initially tried was to reshape the Matrix H into a tensor of rank m_1\times m_2 \times n_1 \times n_2 using `reshape`

and then swap the second and third index using `permutedims`

and then rearrange it back into a matrix using `reshape`

again.

```
H1 = reshape(H,(m1,m2,n1,n2))
H2 = permutedims(H1,(1,3,2,4))
H3 = reshape(H2,(m1*n1,m2*n2))
```

However, `reshape`

doesn’t rearrange the elements as I want them to. For example, if I run the procedure above on the example given in the math.stackexchange answer, I get:

instead of what is needed:

So, my question is: Is there an easy/obvious way of doing this transformation or am I stuck with writing a depth 4 nested `for`

loop?