How can I pad zeros to each column of a matrix?

22 Ansichten (letzte 30 Tage)
Luki
Luki am 27 Dez. 2016
Kommentiert: Image Analyst am 27 Dez. 2016
I am given a matrix with t rows and n columns. I want to add zeros to each column. How can I achieve this? I was thinking about the padarray-command:
X_padded = padarray(X,[10000 0]);
But I think this yields a matrix X with only zeros added to it in column n=1.

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Dez. 2016
Here are two easy ways to add zeros onto a matrix:
>> mat = [1,2,3;4,5,6]
mat =
1 2 3
4 5 6
>> [mat;zeros(2,3)]
ans =
1 2 3
4 5 6
0 0 0
0 0 0
>> mat(5,1) = 0
mat =
1 2 3
4 5 6
0 0 0
0 0 0
0 0 0
  2 Kommentare
Luki
Luki am 27 Dez. 2016
allright, thx! There's one problem:
size(x) = 10000 1 41
so, I could do:
x_zeros = [x,1,zeros(100,41)]
But I'd rather get rid of the third dimension of x (the 1 depicted in size(x)). I know it's not a real 3rd dimension, because the "depth" of the matrix is just 1. But every other assignment in my code will only use two indices (for columns and rows). For x as it is right now, however, I need three indices. Can I delete that additional dimension?
Image Analyst
Image Analyst am 27 Dez. 2016
Try getting rid of the third dimension, if you don't need or want it, like this:
x = squeeze(x);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by