How can Write a function that combines two lists by alternatingly taking elements, e.g. [23,11,70], [1,2,3] → [23,1,11,2,70,3].

 採用された回答

Using the reshape function:
A = [23,11,70];
B = [1,2,3];
Out = reshape([A; B], 1, [])
Out =
23 1 11 2 70 3

2 件のコメント

anas birrow
anas birrow 2017 年 11 月 10 日
thanks for ur help but i need this code in function to calling it
I am not certain what you are asking.
See if this does what you want:
function C = concat(L1,L2)
C = reshape([L1; L2], 1, [])
end
or using an anonymous function:
concat = @(L1,L2) reshape([L1; L2], 1, []);

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

その他の回答 (2 件)

KL
KL 2017 年 11 月 10 日
No need to use a loop,
A = [1 2 3 4 5];
>> B = 10*A;
>> C = zeros(size([A B]));
>> C(1:2:end) = A;
>> C(2:2:end) = B;
>> C
C =
1 10 2 20 3 30 4 40 5 50

カテゴリ

ヘルプ センター および 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