How to resize / expand a matrix by adding zeros?

arrays, matlab, matrix

Solution

you can add zeros to a matrix using `padarray`... For example:

A = [1 2; 3 4];
B = padarray(A,[2 2],'post')

B =
 1     2     0     0
 3     4     0     0
 0     0     0     0
 0     0     0     0

Or, if you don't have the image processing toolbox, you can use matrix indexing:

B = zeros(size(A)+k, class(A));
B(k:end-k+1,k:end-k+1) = A;

Problem

How is it possible to expand a quadratic - let's say NxN - matrix to a bigger on like a (N+k)x(N+k) matrix? It's really all about resizing the matrix and filling the missing rows/columns with zeros such that not dimension mismatch occurs.

Original source