Concatenation of vectors in a specific, non-serial order

I have vectors a, band c; vectors aand bcontain integers, and the vector chas binary values ​​as elements: ( 0, 1).

The vector ahas a length n, and the vector bhas a length k. The vector chas a length of n + k.

I want to concatenate vectors aand bbased on a vector c.

For example. If c=[1 0 0 1 0 . . . . ], then I want to create a vectorres=[a(1) b(1) b(2) a(2) b(3) . . . ].

Is there a way to do this without a loop for?

+4
source share
1 answer
res = c; %// copy c for the result vector
res(c) = a;
res(~c) = b;

! , 0 c b, 1 a. @Dan comment

+7

Source: https://habr.com/ru/post/1624085/


All Articles