Create a new array from an intersection of two previous arrays

2 visualizaciones (últimos 30 días)
luca
luca el 3 de Oct. de 2019
Editada: Stephen23 el 3 de Oct. de 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.

Respuesta aceptada

Stephen23
Stephen23 el 3 de Oct. de 2019
Editada: Stephen23 el 3 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by