How to take alternating elements from two vectors?
Show older comments
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
KSSV
on 24 Apr 2018
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
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!