MATLAB: How tonsert Column and Row in all dimensions of a matrix

matrix

For Example I have a matrix-
A = [1 2 3; 4 5 6; 7 8 9]
now i need a matrix
B = [1 1 1 1 1;1 1 2 3 1;1 4 5 6 1; 1 7 8 9 1; 1 1 1 1 1]
Means i need to add an equal number of elements around the matrix. Thanks in advance.

Best Answer

[rows, cols] = size(A);
B = [ones(1, cols+2); ones(rows, 1), A, ones(rows, 1); ones(1,cols+2)]
Alternately if you have the Image Processing Toolbox,
B = padarray(A,[1,1],1);