フィルターのクリア

How to add zeros to the end of a column vector

26 ビュー (過去 30 日間)
john iceton
john iceton 2021 年 9 月 7 日
コメント済み: Image Analyst 2021 年 9 月 8 日
I have two colum vectors that of uneven length that need to be added together as seen below:
a =
1
2
3
4
5
6
7
8
9
b =
1
2
3
4
5
How can I pad colum b from 5 onwards with zeros to allow the vectors to be added ?

回答 (2 件)

Dave B
Dave B 2021 年 9 月 7 日
You can use padarray for this (or you can just do [b; zeros(numel(a)-numel(b),1)])
a = (1:9)'
a = 9×1
1 2 3 4 5 6 7 8 9
b = (1:5)'
b = 5×1
1 2 3 4 5
c=padarray(b,numel(a)-numel(b),0,'post')
c = 9×1
1 2 3 4 5 0 0 0 0
c2=[b; zeros(numel(a)-numel(b),1)] % alternate
c2 = 9×1
1 2 3 4 5 0 0 0 0
a+c
ans = 9×1
2 4 6 8 10 6 7 8 9
  1 件のコメント
Dave B
Dave B 2021 年 9 月 7 日
you can even set the last value to zero and matlab will fill in the blanks:
a = (1:9)';
b = (1:5)';
b(numel(a)) = 0
b = 9×1
1 2 3 4 5 0 0 0 0
a+b
ans = 9×1
2 4 6 8 10 6 7 8 9

サインインしてコメントする。


Chien Poon
Chien Poon 2021 年 9 月 7 日
b(length(b)+1:length(a)) = 0;
  1 件のコメント
Image Analyst
Image Analyst 2021 年 9 月 8 日
Or more simply:
b(length(a)) = 0

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by