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

22 次查看(过去 30 天)
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.

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by