How to run find function faster?

29 visualizaciones (últimos 30 días)
Benson Gou
Benson Gou el 26 de Jun. de 2021
Comentada: dpb el 29 de Jun. de 2021
Dear All,
I have an array A with 6000 elements in integers, for example, "10", "12", "5" and others. Actually array A saves the number of non-zero elements in each rows of a sparse matrix B. I want to do some calculations on those selected rows in B with certian number of non-zero elements. I want to find out the indecis of selected values "10", "12" and "5" in A.
I did the following:
[~,ind1] = find(A==10);
[~,ind2] = find(A==12);
[~,ind3] = find(A==5).
I found it took about 1 second which is too slow. I am wondering if there is a faster way to do this
Thanks a lot.
Benson
  6 comentarios
Paul
Paul el 29 de Jun. de 2021
Is anyone else surprised that these three calls to find takes "about 1 second" for an 6000-element array of integers?
dpb
dpb el 29 de Jun. de 2021
Not without more details; "know nuthink!" of just how was timed or what was the platform.
Also, I've not compared, but that the A array is sparse could well be a factor as well...

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 26 de Jun. de 2021
Generally one doesn't need find explicitly at all; just use the logical addressing vector returned by the logical test.
Alternatives for something like the above would quite possibly be findgroups and splitapply depending upon just what is to be done next.
Or, of course, there are all the binning functions such as discretize that might come into play.
Without the end use here, it's hard to say specifically.
One note, however, is that creating sequentially-numbered variables such as above is generally a sign that the code factorization is not optimal in MATLAB -- arrays or cell arrays would be far better in all likelihood. Once you've created such a set of variables, you've made using them difficult for anything other than just duplicating the same code over and over...
  2 comentarios
Benson Gou
Benson Gou el 28 de Jun. de 2021
Hi, dpb,
Thanks for your reply. But I still do not know how to do it after several times tries.
Best regards,
Benson
dpb
dpb el 28 de Jun. de 2021
Well, we've got to see what you're trying, specifically, to be able to answer specifically.
No code, not even an error message isn't much for even the crystal ball to work with...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Language Fundamentals 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