how to add zeros around a matrix?
Show older comments
for example i have a matrix A = [1 2 3; 2 3 4; 5 6 7] I am working on image processing and I want to add zeros around this matrix like below. How can i do that?
0 0 0 0 0
0 1 2 3 0
0 2 3 4 0
0 5 6 7 0
0 0 0 0 0
Accepted Answer
More Answers (3)
Andrei Bobrov
on 22 Nov 2017
Edited: Andrei Bobrov
on 22 Nov 2017
padarray(A,[1 1],0)
2 Comments
Stephen23
on 22 Nov 2017
Note: requires the image toolbox.
DGM
on 16 Jul 2022
While padarray() requires IPT, MIMT has padarrayFB(), which uses, but does not require IPT. That's a possibility for those who don't have IPT. ... but then MIMT also has other options for adding borders to images.
noam gridish
on 12 Mar 2024
0 votes
% create bigger mat of zeros
[m,n]=size(A);
paded_mat=zeros(m+1,n+1);
paded_mat(2:end-1,2:end-1)=A;
A=paded_mat;
1 Comment
DGM
on 13 Mar 2024
Have you tested this? I ask because it will throw an error. Your preallocated array is too small.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!