MATLAB: How can I add zeros elements to a matrix

matrix

I have a matrix a: a= [1 2 3; 1 2 3; 1 2 3] I want to add zero rows and cols and convert it to b:
b=
1 2 3 0 0
1 2 3 0 0
1 2 3 0 0
0 0 0 0 0
0 0 0 0 0
how is it possible?
(size of 'a' can be different, in fact it's a matrix which shows image m*n)

Best Answer

Hi Moein,
there are different ways to do this. One of which is to define a fully zero matrix with all zeros
newMatrix = zeros(5,5); % all zero matrix
Then assign the non zero elements.
newMatrix(1:3,1:3) = oldMatrix; % oldMatrix is a, newMatrix is b
This is one way to do it.
Another way would be to concatenate the oldMatrix with zeros.
neMatrix_1 = [oldMatrix,zeros(3,2)]; % horizontal concatenation
newMatrix_2 = [newMatrix_1;zeros(2,5)]; % vertical concatenation
This should give you the same in newMatrix_2