How to combine 2 vectors of unequal length
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (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 Commenti
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
il 5 Set 2015
Categorie
Scopri di più su Digital Filter Analysis in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!