How to add zeros to the end of a column vector
Show older comments
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 ?
Answers (2)
a = (1:9)'
b = (1:5)'
c=padarray(b,numel(a)-numel(b),0,'post')
c2=[b; zeros(numel(a)-numel(b),1)] % alternate
a+c
1 Comment
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
a+b
Chien Poon
on 7 Sep 2021
0 votes
b(length(b)+1:length(a)) = 0;
1 Comment
Image Analyst
on 8 Sep 2021
Or more simply:
b(length(a)) = 0
Categories
Find more on Creating and Concatenating Matrices 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!