Insert elements of one vector into another in a specific order

8 visualizaciones (últimos 30 días)
Dc
Dc el 19 de Abr. de 2021
Comentada: Dc el 19 de Abr. de 2021
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.
Thanks in advacne.
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.

Respuesta aceptada

David Hill
David Hill el 19 de Abr. de 2021
B(end+1)=B(end);%B needs 1 more element, just duplicated last element
c=[reshape(A,124,[]);reshape(B,2,[])];
c=c(:)';

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by