reshape array to matrix from row to column in matlab

To reshape an array to matrix with column order in Matlab, you can use the reshape function as follows:

main.m
% create an example array
array = [1, 2, 3, 4, 5, 6, 7, 8, 9];

% reshape to matrix with 3 columns
matrix = reshape(array, [], 3);
131 chars
6 lines

In this example, [] means that the number of rows is automatically calculated based on the number of columns. The resulting matrix will have 3 columns, and the number of rows will be calculated as numel(array)/3. If the input array has a different number of elements, you need to adjust the second argument of reshape accordingly.

Note that the elements of the input array are placed into the matrix in column-major order. If you want row-major order instead, you can transpose the matrix:

main.m
matrix = reshape(array, [], 3)';
33 chars
2 lines

related categories

gistlibby LogSnag