Create a new array from an intersection of two previous arrays

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by