Effacer les filtres
Effacer les filtres

How to combine 2 vectors of unequal length

14 vues (au cours des 30 derniers jours)
Lorraine Williams
Lorraine Williams le 5 Sep 2015
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?

Réponse acceptée

Star Strider
Star Strider le 5 Sep 2015
Modifié(e) : Star Strider le 5 Sep 2015
One possibility:
A = [1 2 3];
B = [5 6 7 8 9 22 13];
LenA =length(A);
C = zeros(1, LenA+length(B));
C(1:2:LenA*2) = A;
C(2:2:LenA*2) = B(1:LenA);
C(LenA*2+1:end) = B(LenA+1:end)
C =
1 5 2 6 3 7 8 9 22 13
EDIT Originally reversed orders of ‘A’ and ‘B’ in ‘C’. Correct now.
  4 commentaires
Lorraine Williams
Lorraine Williams le 5 Sep 2015
So, as this is all new to me, could I impose and ask if you could explain each step for me - I sort of get it but when it gets to LenA*2 then I'm not sure what's going on :)
Star Strider
Star Strider le 5 Sep 2015
Modifié(e) : Star Strider le 5 Sep 2015
No imposition at all! This is MATLAB Answers!
The LenA call is just the length of the shorter vector, ‘A’. I use that later in the code, so it’s more efficient to do the operation once and then use that result.
The ‘C’ assignment creates the initial vector with a length that is the sum of the lengths of its two components.
This next is an introduction to the colon (:) operator. It defines integer vectors here, although it has other uses. (See Special Characters for details.)
The ‘C(1:2:LenA*2) = A;’ assignment uses the colon operator to begin with 1, step by 2, and end with ‘LenA*2’ because we want ‘C’ to have the elements of ‘A’ as its first, third, and fifth elements. This creates a vector of indices: [1 3 5]. Because this is by definition going to be twice the length of ‘A’, we stop it at ‘LenA*2’.
The next assignment, ‘C(2:2:LenA*2) = B(1:LenA);’ does essentially the same thing, however it starts at C(2) with a step of 2 to fill ‘C([2 4 6])’ with the first three elements of ‘B’. (Note that ‘C([2 4 6])’ is also correct MATLAB code.)
At this point the code have created ‘C(1:6)’, or ‘C(1:LenA*2)’ from all of ‘A’ and the first ‘LenA’ elements of ‘B’, that is ‘B(1:LenA)’. Since the rest of ‘C’ (that is, ‘C(LenA*2+1:end)’) are the remaining elements of ‘B’ (that is, ‘B(LenA+1:end)’) , we assign:
C(LenA*2+1:end) = B(LenA+1:end)
I didn’t assign steps here because the step is assumed to be +1 unless specifically defined as something else. I left off the end semicolon because I wanted to print the output so I could post it.

Connectez-vous pour commenter.

Plus de réponses (1)

Cedric
Cedric le 5 Sep 2015
Modifié(e) : Cedric le 5 Sep 2015
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 commentaires
Cedric
Cedric le 5 Sep 2015
Modifié(e) : Cedric le 5 Sep 2015
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
Lorraine Williams le 5 Sep 2015
Thank you so much for helping and being so responsive! I'm new to this and it's a bit daunting!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by