Skip to content

reshape

Reshape an array without changing its data.

B = reshape(A, m, n)
B = reshape(A, m, n, p, ...)
B = reshape(A, [m n ...])

Returns an array with the requested dimensions, drawing elements from A in column-major order. The product of the new dimensions must equal numel(A); otherwise an error is raised.

Use one dimension as [] to have it computed automatically: reshape(A, 3, []) makes a 3-row matrix with as many columns as needed.

reshape(1:12, 3, 4) % 3×4 matrix
reshape(1:12, [2 3 2]) % 2×3×2 array
reshape(magic(4), 2, []) % 2×8 (auto-computed columns)
  • squeeze — Remove singleton dimensions.
  • permute — Rearrange dimensions of an N-D array.
  • repmat — Tile an array by replicating its contents.