reshape
Changes the size of an array without changing its elements.
Category: Language Fundamentals > Matrices and Arrays
Syntax
B = reshape(A, m, n, ...)
Arranges the elements into the given dimensions.
B = reshape(A, [], n)
Leaves one size empty and lets MATLAB compute it.
B = reshape(A, sz)
Size given as a vector.
Description
The elements are read down the columns of A and written down the columns of the new shape, so reshape(1:6, 2, 3) is [1 3 5; 2 4 6], not [1 2 3; 4 5 6]. To fill row by row, reshape to the transposed size and transpose back.
If the number of elements would change the call fails with an error; the [] form is the easy way to avoid computing a size by hand.
Input arguments
A- Array of any type.
m, n, ...- New dimension sizes; the product must equal
numel(A). At most one may be[].
Output arguments
B- Array with the same elements in column-major order.
Example
reshape(1:6, 2, 3)
reshape(1:6, [], 2)
reshape(1:6, 3, 2)'
Output
ans = 2×3
1 3 5
2 4 6
ans = 3×2
1 4
2 5
3 6
ans = 2×3
1 2 3
4 5 6