# how to make zero padding?

67 visualizzazioni (ultimi 30 giorni)
sheno39 il 8 Ott 2013
Commentato: Image Analyst il 5 Apr 2016
can anyone help me to make zero padding for a 4x3 matrix. i have to add a row of zeros in the top,bottom, left and right side of the matrix
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Image Analyst il 8 Ott 2013
If you have the Image Processing Toolbox, you can use padarray().
##### 2 CommentiMostra NessunoNascondi Nessuno
sheno39 il 8 Ott 2013
Thank You Sir.
Jie il 9 Ott 2013
cool

Accedi per commentare.

### Più risposte (7)

cr il 8 Ott 2013
Modificato: cr il 8 Ott 2013
m = [zeros(1,size(m,2)+2); [zeros(size(m,1),1), m, zeros(size(m,1),1)]; zeros(1,size(m,2)+2)];
or even simpler, but needs creating a temp variable:
tmp = zeros(size(m)+2);
tmp(2:end-1,2:end-1) = m;
m = tmp;
Cheers.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Jos (10584) il 8 Ott 2013
Modificato: Jos (10584) il 8 Ott 2013
Pad a matrix M with N zeros to all sides using indexing (neither trivial nor boring):
M = ceil(10*rand(3,4))
N = 2
if N > 0
M(end+2*N,end+2*N) = 0
M = M([end-N+1:end 1:end-N], [end-N+1:end 1:end-N])
end
If you have the image toolbox, take a look at PADARRAY.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Slim Ben Ghalba il 21 Nov 2013
a smarter and simpler way. here is an example for zero-padding with 4:
>> a=zeros(1,12)
a =
0 0 0 0 0 0 0 0 0 0 0 0
>> a(1:4:end)=1
a =
1 0 0 0 1 0 0 0 1 0 0 0
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Image Analyst il 15 Lug 2014
This does not wrap the matrix with a layer of zeros on all sides like the original poster asked or like padarray() does.

Accedi per commentare.

Jie il 8 Ott 2013
Modificato: Jie il 8 Ott 2013
% just wish you matrix size is not too large
[n m]=size(in_put);
A=['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' ];
B=1:20;
str1=[A(m+2), num2str(B(n+2))];
str2=['b2:' A(m+1), num2str(B(n+1))];
str3=['a1:' str1];
out_put(isnan(out_put))=0;
could take some time, but save much effort regarding the mat size. I think the string part could be optimized somehow, but i just don't know how. Also u can try some simple but boring methods. like
a(1,2:end-1)=ones(1,size(a,2)-1)
or the alike.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

FRANCISCO JAVIER il 8 Ott 2013
M=rand(2,1);
K=[zeros(2,1) M zeros(2,1)];
N=[zeros(1,3); K; zeros(1,3)];
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Kiarash Ahi il 15 Lug 2014
Modificato: Kiarash Ahi il 15 Lug 2014
Try this as well:
[M N]=size(k);
for n=1:2:N
k(:,n)=0
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Md. Kamal Hossain il 5 Apr 2016
How to add zero in a sequence
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Image Analyst il 5 Apr 2016
If the sequence is stored in a vector, and index is the location where you want to add/insert the zeros:
newVec = [vec(1:index-1), 0, vec(index:end)];

Accedi per commentare.

### Categorie

Scopri di più su Digital Filtering in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by