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

13 views (last 30 days)
Dc on 19 Apr 2021 at 15:24
Commented: Dc on 19 Apr 2021 at 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 on 19 Apr 2021 at 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(:)';
Dc on 19 Apr 2021 at 15:44
Thank you very much! It worked perfectly.