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

22 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 27 Dec 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 Comments
Luki
Luki on 27 Dec 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 on 27 Dec 2016
Try getting rid of the third dimension, if you don't need or want it, like this:
x = squeeze(x);

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!