sfendell
sfendell

Reputation: 5595

Reshaping a matrix in matlab

I've got a matrix in matlab that's 28x28x10000. I'm looking to reshape it into a matrix that's 10000*784, with each 28x28 submatrix being squeezed into a row. So I tried

reshape(mat, 10000, 784)

While this does give me a matrix of the correct shape, the values aren't correct. Does anyone know of another way to do this, preferably without for loops?

Upvotes: 1

Views: 644

Answers (1)

tmpearce
tmpearce

Reputation: 12683

reshape take elements column-wise from the matrix. For your purpose, that means that if you rearrange the dimensions of your original matrix (using permute), you can use reshape like you already are:

reshape(permute(mat,[3 1 2]), 10000, []);

The [3 1 2] argument to permute means use the 3rd dimension as the 1st, then the original 1st as the new 2nd, and the original 2nd dimension as the new 3rd one, giving you a 10000x28x28 matrix. Each column contains 10000 elements, so taking column-by-column like reshape does will work give you what you want.

Upvotes: 3

Related Questions