How to combine 2 vectors of unequal length
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (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 comentarios
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
el 5 de Sept. de 2015
Categorías
Más información sobre Digital Filter Analysis en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!