How to take alternating elements from two vectors?

I am very new to Matlab
a1 = [1 2 3 4 5 6 7]
a2 = [11 22 33 44 55 66 77]
i need a3 to be
a3 = [1 22 3 44 5 66 7]
how do i get a3?

Answers (2)

Simpler:
>> a3 = a1;
>> a3(2:2:end) = a2(2:2:end)
a3 =
1 22 3 44 5 66 7
a1 = [1 2 3 4 5 6 7]
a2 = [11 22 33 44 55 66 77]
% i need a3 to be
a3 = [1 22 3 44 5 66 7]
iwant = zeros(size(a1)) ;
iwant(1:2:end) = a1(1:2:end) ;
iwant(2:2:end) = a2(2:2:end) ;

Categories

Edited:

on 24 Apr 2018

Community Treasure Hunt

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

Start Hunting!