Combine two vectors together

4 views (last 30 days)
HAIRU WANG
HAIRU WANG on 20 Apr 2020
Answered: Bruno Luong on 28 Aug 2020
For example I got a = [1 3 5], b = [2 4] and I want to get a c = [1 2 3 4 5 ]
Basically it'd insert the vaule from b between a's two elements

Answers (3)

KSSV
KSSV on 20 Apr 2020
iwant = sort([a, b])

Bruno Luong
Bruno Luong on 28 Aug 2020
a(1:2:2*end-1) = a;
a(2*(1:length(b))) = b

Osip Vayner
Osip Vayner on 28 Aug 2020
KSSV's code is correct.
And just in case you want to combine two vectors vertically:
d = sort([a;b])

Products

Community Treasure Hunt

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

Start Hunting!