speed up line: find(c>a(:,1) & c<= a(:,2))

How can I speed up this line:
a=[3 4; 5 6; 4 5;2 8; 1 9; 23 24;1 12;3 6;12 25; 15 30;...]
b=find(5>a(:,1) & 5<= a(:,2));
when size(a) is 100,000,000 *2, this line is super slow. Any idea?
Thank you!

6 commentaires

Stephen23
Stephen23 le 25 Mar 2018
Modifié(e) : Stephen23 le 25 Mar 2018
Logical indexing is faster than using find. Is it possible to use logical indexing in this situation?
Chaoyang Jiang
Chaoyang Jiang le 25 Mar 2018
Maybe no as I need the index, and do the for loop after.
When you say 'super slow' what sort of order are we talking about? Out of curiosity I tried this on a 100 million by 2 dummy array of integers and was getting just over a second on the find function and a little over half a second creating a logical indexing array
%dummy array 100 million by 2 elements
a=randi(100,100000000,2);
tic
b=find(5>a(:,1) & 5<= a(:,2));
toc
tic
c=(5>a(:,1)) & (5<=a(:,2));
toc
results:
Elapsed time is 1.043276 seconds.
Elapsed time is 0.687334 seconds.
Chaoyang Jiang
Chaoyang Jiang le 25 Mar 2018
Modifié(e) : Chaoyang Jiang le 25 Mar 2018
It won't take very long time to call this line once. However, in my code, I need to call this line over 700,000 times. Then the speed becomes a problem. I used profile to check the running time, and 85% running time is spent on this line.
David Fletcher
David Fletcher le 25 Mar 2018
In that case, I think shaving a few tenths off here and there isn't going to make much of a dent in your issue. Fundamentally, you are trawling through millions upon millions of elements hundreds of thousands of times. I've had little experience of the parallel toolbox, so I'm not sure if that could help in some way, but I suspect it would be like chucking chisels at Everest.
Jan
Jan le 25 Mar 2018
Then it is time to think twice: Is this approach efficient? Do you have any further information, which can be exploited, e.g. if the data is sorted, or the positions are near to the ones of the last iteration? A brute search is time-consuming in large data. There is no magic trick to reduce the time. Maybe you can parallelize the iterations?
I assume you can get more help, if you explain the actual problem and post the relevant code.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Tags

Commenté :

Jan
le 25 Mar 2018

Community Treasure Hunt

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

Start Hunting!

Translated by