TExcelTAD
Home › MATLAB / Octave › Reshaping & Combining Arrays

Reshaping & Combining Arrays

Data rarely arrives in exactly the shape you need. This lesson covers the functions that rearrange arrays without changing their values: concatenating, reshaping, flipping, rotating, shifting and repeating.

Concatenation

You have already used square brackets to join arrays. [A B] joins horizontally (same number of rows), [A; B] joins vertically (same number of columns). The functions horzcat, vertcat and cat(dim, ...) do the same thing:

matlab · try it yourself
A = [1 2; 3 4];
B = [5; 6];
C = [7 8];
disp([A B])
disp([A; C])
disp(isequal(horzcat(A, B), [A B]))
disp(isequal(cat(1, A, C), [A; C]))
try
    bad = [A C];
catch err
    disp('[A C] fails: A has 2 rows, C has 1')
end
   1   2   5
   3   4   6
   1   2
   3   4
   7   8
1
1
[A C] fails: A has 2 rows, C has 1

reshape

reshape(A, m, n) lays the same elements out as an m-by-n array. The total number of elements must stay the same. Elements are taken and placed in column order, the same order as linear indexing:

matlab · try it yourself
v = 1:6;
disp(reshape(v, 2, 3))
disp(reshape(v, 3, 2))
disp(reshape(v, [], 2))    % [] means "work this dimension out"
   1   3   5
   2   4   6
   1   4
   2   5
   3   6
   1   4
   2   5
   3   6

If you want the numbers to fill across rows instead, reshape to the transposed size and transpose: reshape(v, 3, 2)' gives [1 2 3; 4 5 6].

Flattening

A(:) returns every element as one column. That is the most common reshape of all:

matlab · try it yourself
M = [1 2; 3 4];
col = M(:)
fprintf('Total of all elements: %d\n', sum(M(:)));
col =

   1
   3
   2
   4

Total of all elements: 10

Flipping, rotating and shifting

Function Effect
fliplr(A) Reverse the order of columns (left-right)
flipud(A) Reverse the order of rows (up-down)
rot90(A) Rotate 90° counter-clockwise (rot90(A, k) for k turns)
circshift(A, k) Shift elements by k positions, wrapping around
sort(A) Sort each column (or a vector)
matlab · try it yourself
A = [1 2 3; 4 5 6];
disp(fliplr(A))
disp(flipud(A))
disp(rot90(A))
disp(circshift([1 2 3 4 5], 2))
disp(circshift([1 2 3 4 5], -1))
   3   2   1
   6   5   4
   4   5   6
   1   2   3
   3   6
   2   5
   1   4
   4   5   1   2   3
   2   3   4   5   1

Repeating

repmat(A, m, n) tiles the whole array. repelem(v, k) repeats each element k times:

matlab · try it yourself
disp(repmat([1 2], 1, 3))
disp(repelem([1 2], 3))
disp(repelem([7 8 9], [1 2 3]))
   1   2   1   2   1   2
   1   1   1   2   2   2
   7   8   8   9   9   9

Size queries

Function Returns
size(A) [rows cols]
size(A, 1) / size(A, 2) Rows / columns only
numel(A) Number of elements
ndims(A) Number of dimensions (at least 2)
isempty, isscalar, isvector, ismatrix Shape tests

Arrays can have more than two dimensions too. zeros(2, 3, 4) is a stack of four 2-by-3 pages, indexed as A(row, col, page):

matlab · try it yourself
stack = zeros(2, 3, 4);
stack(:, :, 2) = 1;
disp(size(stack))
disp(ndims(stack))
disp(numel(stack))
disp(sum(stack(:)))
   2   3   4
3
24
6

Test yourself

What is reshape(1:6, 2, 3)?

  1. [1 2 3; 4 5 6]
  2. [1 3 5; 2 4 6]
  3. [1 4; 2 5; 3 6]
  4. An error

What does circshift([1 2 3], 1) return?

  1. [2 3 1]
  2. [3 1 2]
  3. [1 2 3 1]
  4. [0 1 2]

Which call repeats each element of [4 5] twice, giving [4 4 5 5]?

  1. repmat([4 5], 1, 2)
  2. repelem([4 5], 2)
  3. [4 5] * 2
  4. reshape([4 5], 2)