필터 지우기
필터 지우기

How to combine 2 vectors of unequal length

조회 수: 14 (최근 30일)
Lorraine Williams
Lorraine Williams 2015년 9월 5일
편집: Walter Roberson 2015년 9월 6일
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?

채택된 답변

Star Strider
Star Strider 2015년 9월 5일
편집: Star Strider 2015년 9월 5일
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
Lorraine Williams
Lorraine Williams 2015년 9월 5일
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 2015년 9월 5일
편집: Star Strider 2015년 9월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cedric
Cedric 2015년 9월 5일
편집: Cedric 2015년 9월 5일
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
Cedric
Cedric 2015년 9월 5일
편집: Cedric 2015년 9월 5일
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 2015년 9월 5일
Thank you so much for helping and being so responsive! I'm new to this and it's a bit daunting!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by