How to combine 2 vectors of unequal length
Show older comments
Hi there
If I have:
A = [1 2 3]
B = [5 6 7 8 9 22 13]
How would I be able to create C=[A(1) B(1) A(2) B(2).....]
such that when you run out of elements in one vector, the new vector also contains the remaining elements from the longer vector?
Accepted Answer
More Answers (1)
Here is one way:
if numel( A ) < numel( B )
C = [B; B] ;
C(1,1:numel( A )) = A ;
else
C = [A; A] ;
C(2,1:numel( B )) = B ;
end
C = C(:)' ;
2 Comments
Seeing Star Strider's answer, I realize that I probably misunderstood the question and that you don't want to use elements of e.g. B for replacing missing elements of A. In other words, you want the output that Star Strider provides, and not:
C = 1 5 2 6 3 7 8 8 9 9 22 22 13 13
Lorraine Williams
on 5 Sep 2015
Categories
Find more on Digital Filter Analysis 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!