This should work:
reshape(permute(A,[1 4 2 3]),[2 8])
To understand this, you can go step by step. First do the following:
reshape(A,[2 8])
ans =
1 2 2 3 10 12 12 13
4 5 5 6 14 15 15 16
You observe that the columns of the reshaped matrix are taken by sliding across the second dimension in the original matrix. After second dimension is over, you slide over to third dimension and the re-iterate over second dimension (here first dimension is rows, second is column, so on...).
What you want to do is, iterate over 4th dimension (as if its a second dimension). You also want (4,14)
after (1,10)
. You can see that corresponding elements vary across second dimension (but reshape
is going to slide over third dimension, no matter what. So swap 2nd and 3rd dimensions).
Finally, you get, reshape(permute(A,[1 4 2 3]),[2 8])
.
I have always had a hard time explaining permute
to somebody. I hope I didn't confuse you more.