Create a new array from an intersection of two previous arrays

2 ビュー (過去 30 日間)
luca
luca 2019 年 10 月 3 日
編集済み: Stephen23 2019 年 10 月 3 日
Hi given the following arrays
A = [1 1 1 1 4 4 4 4 6 6 6 6 6 6]
B = [2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7 7 7 8 8 8 8 9 9 9 9]
I want to create a new vector C that is the intersection of A and B
C = [1 2 1 2 1 2 1 2 4 2 4 2 4 2 4 2 6 2 6 2 6 7 6 7 6 7 6 7 7 7 7 7 8 8 8 8 9 9 9 9 ]
So C is created by insert one value of B after one value of A, till the end.

採用された回答

Stephen23
Stephen23 2019 年 10 月 3 日
編集済み: Stephen23 2019 年 10 月 3 日
>> A = [1 1 1 1 4 4 4 4 6 6 6 6 6 6];
>> B = [2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7 7 7 8 8 8 8 9 9 9 9];
>> N = min(numel(A),numel(B));
>> C = [reshape([A(1:N);B(1:N)],1,2*N),A(N+1:end),B(N+1:end)]
C =
1 2 1 2 1 2 1 2 4 2 4 2 4 2 4 2 6 2 6 2 6 7 6 7 6 7 6 7 7 7 7 7 8 8 8 8 9 9 9 9

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by