Create a new array from an intersection of two previous arrays

2 Ansichten (letzte 30 Tage)
luca
luca am 3 Okt. 2019
Bearbeitet: Stephen23 am 3 Okt. 2019
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 3 Okt. 2019
Bearbeitet: Stephen23 am 3 Okt. 2019
>> 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by