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

1 回表示 (過去 30 日間)
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.

採用された回答

Star Strider
Star Strider 2019 年 3 月 22 日
編集済み: Star Strider 2019 年 3 月 22 日
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
  2 件のコメント
Brittny Freeman
Brittny Freeman 2019 年 3 月 22 日
You're the best Star, it works perfectly!
Star Strider
Star Strider 2019 年 3 月 22 日
As always, my pleasure! Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by