# Insert elements of one vector into another in a specific order

13 ビュー (過去 30 日間)
Dc 2021 年 4 月 19 日 15:24
コメント済み: Dc 2021 年 4 月 19 日 15:44
Hello everyone,
I have a vector A=[A1 A2 A3 ... A30876] of length(A)=30876 and a vector B=[B1 B2 B3 ... B497] of length(B)=497. In order to then solve a linear system, I first need to create a vector C of length(C)=length(A)+length(B) by inserting the elements of B into A like this:
C=[A1 A2 A3 ... A 124 B1 B2 A125 A126 ... A248 B3 B4 A249 A250 ... A372 B5 B6 A373 A374 ...] (and so on).
Basically, there have to be two elements of B after every 124 elements of A.
Note: This is actually the exact opposite of a question I asked a couple of weeks earlier here: https://www.mathworks.com/matlabcentral/answers/796337-rearrangement-of-vector-elements and I eventually solved using the concept of logical indexing suggested by Jan (not the exact same code though). Unfortunately, I did not manage to apply the same principle "backwards" to solve this issue.

サインインしてコメントする。

### 採用された回答

David Hill 2021 年 4 月 19 日 15:32
B(end+1)=B(end);%B needs 1 more element, just duplicated last element
c=[reshape(A,124,[]);reshape(B,2,[])];
c=c(:)';
##### 1 件のコメント表示非表示 なし
Dc 2021 年 4 月 19 日 15:44
Thank you very much! It worked perfectly.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by