Borrar filtros
Borrar filtros

Finding the n'th element satisfying a condition

29 visualizaciones (últimos 30 días)
Nirmal
Nirmal el 7 de Mayo de 2012
I have a huge array A (about 30 million elements) and I'm trying to extract the n'th element satisfying a condition A == b. Currently, I' using the following:
x = max(find(A == b,n))
Is there a more efficient way to do this since I need to do this several times in my code?
Thanks.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 7 de Mayo de 2012
That looks good to me. The only thing that might make it faster would be to index instead of use max():
idx = find(A==B,n,'first')
idx(end)
  1 comentario
Nirmal
Nirmal el 7 de Mayo de 2012
Using this made the code considerably faster, actually..

Iniciar sesión para comentar.

Más respuestas (2)

Thomas
Thomas el 7 de Mayo de 2012
Seems to be the fastest way of doing this.. Index might make it faster (though I doubt it..) try timing it..

Walter Roberson
Walter Roberson el 7 de Mayo de 2012
I am not sure why you are using max() ?
t = find(A == b, n, 'first');
t(end)
If you are doing this repeatedly, what is varying, and what is known at the time you want to start the first search? If you know all the values you will be searching on, then the three-output version of ismember() might be appropriate.
  1 comentario
Nirmal
Nirmal el 7 de Mayo de 2012
Thanks. I'll also try out the ismember. Avoiding max helped.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by