matlabmatrixrow-major-ordercolumn-major-order

How to get Matrix from row-major vector in Matlab?


I have the following matrix:

50,60,55,67,70
62,65,70,70,81
72,66,77,80,69

I turn now the matrix into a vector but in row-major. This gives the following vector:

50,60,55,67,70,62,65,70,70,81,72,66,77,80,69

Now I would like to turn this vector into the same matrix as above. The problem is that reshape(matrix,[3,5]) does not work because Matlab operates column-major.

How can this be done efficiently (for large matrices)?


Solution

  • To solve this, use

    reshape(matrix,[5,3]).'
    

    First using reshape with row and column dimension swapped, you get a matrix with the right order but transposed, then using transpose you get the right output.

    Having the control systems toolbox, you could also use vec2mat