MATLAB Answers

Adding a frame of zeros to a matrix

17 views (last 30 days)
Rashid Hussein
Rashid Hussein on 4 Apr 2019
Commented: Guillaume on 4 Apr 2019
I have a matrix 19x9 I want to add a frame of zeros so I will be 20x10 ?????

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 4 Apr 2019
Not sure which corner you want to pad with zeros. I'm assuming bottom right:
newmatrix = [yourmatrix, zeros(size(yourmatrix, 1), 1); zeros(1, size(yourmatrix, 2) + 1)];
If you have the image processing toolbox:
newmatrix = padarray(yourmatrix, [1 1], 'direction', 'post');

  2 Comments

Rashid Hussein
Rashid Hussein on 4 Apr 2019
Actually I want to add the zeros as frame for all corners So the original matrix would be surrendered by zeros like square
Guillaume
Guillaume on 4 Apr 2019
newmatrix = [zeros(1, size(yourmatrix, 2) + 2); zeros(size(yourmatrix, 1), 1), yourmatrix, zeros(size(yourmatrix, 1), 1); zeros(1, size(yourmatrix, 2) + 2)];
Using padarray:
newmatrix = padarray(yourmatrix, [1 1]);

Sign in to comment.

More Answers (0)


Translated by