How to merge first elements from 2 array into an new array?

Hi,
I want to merge 2 arrays so that the output is as follows:
A=[1 2 3]
B=[4 5 6]
I want C to be:
C=[1 4 2 5 3 6]
That pick up each successive numbers from each side and place them side by side in a new array? Any way of doing this using simple lines ..if possible no use of inbuilt functions..

1 件のコメント

yashvin
yashvin 2015 年 6 月 30 日
please find below my code:
A=[1 2 3]
B=[4 5 6]
out_weight=zeros(1,2*length(A))
k=1
for i=1:length(A)
out_weight(k)=A(i)
out_weight(k+1)=B(i)
k=k+2
end
Any shorter code i can use?

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 30 日

0 投票

C=reshape([A;B],1,[])

3 件のコメント

yashvin
yashvin 2015 年 6 月 30 日
Hi Azzi,
Thanks for your input Any other way of doing it without using inbuilt function?
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 30 日
編集済み: Azzi Abdelmalek 2015 年 6 月 30 日
C=[A;B]
C=C(:)'
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 30 日
Or
A=[1 2 3]
B=[4 5 6]
n=numel(A)
C(1:2:2*n)=A;
C(2:2:2*n)=B;
Andrei Bobrov
Andrei Bobrov 2015 年 6 月 30 日

0 投票

AB = [A;B];
out = AB(:)';

この質問は閉じられています。

タグ

質問済み:

2015 年 6 月 30 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by