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

22 ビュー (過去 30 日間)
Luki
Luki 2016 年 12 月 27 日
コメント済み: Image Analyst 2016 年 12 月 27 日
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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by