## Alternation without for loop

### JamJan (view profile)

on 15 Oct 2019
Latest activity Commented on by J. Alex Lee

### J. Alex Lee (view profile)

on 15 Oct 2019
I have 2 vectors that I want to alternate
A = [1 2 3 4 5 6 7 8 9 10]
B = [0.5 0.2 0.4 0.8 0.9]
I would like to have the following output (preferably without for loop)
Output = [1 0.5 2 0.2 3 0.4 4 0.8 5 0.9 6 7 8 9 10]
How to do this?
Thanks!

### Stephen Cobeldick (view profile)

on 15 Oct 2019
Edited by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 15 Oct 2019

>> N = min(numel(A),numel(B));
>> V = [reshape([A(1:N);B(1:N)],1,[]),A(N+1:end),B(N+1:end)]
V =
1 0.5 2 0.2 3 0.4 4 0.8 5 0.9 6 7 8 9 10

J. Alex Lee

### J. Alex Lee (view profile)

on 15 Oct 2019
nice...i like when i didn't see the obvious complete opposite path...and i learned a new thing with the empty argument for reshape

### Jos (10584) (view profile)

on 15 Oct 2019

% a sorting trick
A = [1 2 3 4 5 6 7 8 9 10]
B = [0.5 0.2 0.4 0.8 0.9]
C = [A B] ;
[~,ix] = sort([1:numel(A) 1:numel(B)])
C = C(ix)