merging vectors together with alternating values
116 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I want to merge two vectors like this:
A=[ 1; 2; 3; 4;] B=[5; 6; 7; 8;]
resulting vector
C=[1; 5; 2; 6; 3; 7; 4; 8;]
Thank you
0 件のコメント
回答 (4 件)
KALYAN ACHARJYA
2019 年 5 月 28 日
編集済み: KALYAN ACHARJYA
2019 年 5 月 28 日
vec=[A B]';
C=vec(:)
>> A=[ 1; 2; 3; 4]
Example:
>> A=[ 1; 2; 3; 4]
A =
1
2
3
4
>> B=[5; 6; 7; 8]
B =
5
6
7
8
>> vec=[A B]'
vec =
1 2 3 4
5 6 7 8
>> C=vec(:)
C =
1
5
2
6
3
7
4
8
>>
0 件のコメント
Star Strider
2019 年 5 月 28 日
Try this:
A=[1; 2; 3; 4];
B=[5; 6; 7; 8];
C = [A(:) B(:)]';
C = C(:)
producing:
C =
1
5
2
6
3
7
4
8
2 件のコメント
Indrasish Chakraborty
2021 年 8 月 19 日
What if the two vectors are of unequal length ?
For example -
A=[2 4 5 7 8]
B=[3 8 0 1]
Then how to get C=[2 3 4 8 5 0 7 1 8] ?
2 件のコメント
shikhar tyagi
2021 年 8 月 19 日
A=[1; 0; 3; 4 ;6 ;0];
B=[5; 6; 7 ;0 ;4];
C=cat(1,A,B);
C([1:2:end,2:2:end])=C
I hope this helps
Nils Odenwald
2021 年 9 月 2 日
編集済み: Nils Odenwald
2021 年 9 月 2 日
I recommend the MATLAB function "reshape", which allows you to change the order of arrays. You can also manipulate different data types such as strings.
Example:
A = [1; 2; 3; 4; 5];
B = [0; 0; 0; 0; 0];
result = reshape([A B]',[],1)
0 件のコメント
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!