Need to combine every other column of two row vectors of different sizes in MATLAB

1 view (last 30 days)
I have 2 row vectors of different sizes, say:
A = [1 3 5 7 9 11 13 15 17]
B = [2 4 6 8 10 12].
I need to combine vectos A & B to make a new C vector that is the same length of the shorter matrix B such that,
C = [ 1 2 3 4 5 6].
Any help would be very much appreciated, I can't seem to figure out the proper indexing needed to accomplish this in a for loop.

Accepted Answer

Star Strider
Star Strider on 22 Mar 2019
Edited: Star Strider on 22 Mar 2019
One approach:
A = [1 3 5 7 9 11 13 15 17];
B = [2 4 6 8 10 12];
C(1:2:numel(A)*2) = A;
C(2:2:numel(B)*2) = B;
C = C(1:min(numel(A),numel(B)))
producing:
C =
1 2 3 4 5 6

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!