# Can I find set of values in larger set in one step?

3 visualizaciones (últimos 30 días)
huda nawaf el 14 de Ag. de 2022
Respondida: Ishan Ghosekar el 26 de Ag. de 2022
Hi,
I am working on a code that takes a very long time to implement, so I try to optimize it
If I look for a set of values (x) in a larger set (z), can I do that in a way faster than using for and find?
x=[1 2 3];
z=[10 2 1 5 7 .....];
thanks
##### 10 comentariosMostrar 9 comentarios más antiguosOcultar 9 comentarios más antiguos
Bruno Luong el 15 de Ag. de 2022
x=[1 2 3];
z=[10 2 1 5 7];
c=[9 4 2 1];
c(ismember(c,intersect(z,x)))=[]
c = 1×2
9 4

Iniciar sesión para comentar.

Ishan Ghosekar el 26 de Ag. de 2022
As per my understanding, you want to look for the set of values in x in another larger set, z in an optimized way.
• The intersect function returns common values between x and z in sorted order:
>> x=[1 2 3];
>> z=[10 2 1 5 7 4 6 9];
>> intersect(x,z)
ans =
1 2
• The ismember function returns an array containing logical 1 (true) where the data in A is found in B.
>> ismember(x,z)
ans =
1×3 logical array
1 1 0
##### 0 comentariosMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by