MATLAB Answers


Function faster than find?

Asked by Jared
on 8 Apr 2013

I use the find command compare 2 vectors of time, one with a much higher rate than the other. Say vector1=[1 2 3 4 5], vector2 many be [1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.2 2.3 3.1...], but vector2 has an irregular pattern.

What I want to do is find the number of values of vector2 that occur between each point of vector1. So betweem 1 and 2 of vector1, I have 9 values (1.1-1.9). I also want the index of the first value (1.1) which would be 1.

For the second case, between 2 and 3 of vector1, I have 2 values, and the index of the first of those values is 10.

I use the following code to do this:

for i=1:length(vector1)-1
  index=find(vector1(i)<=vector2 & vector2<vector1(i+1),1,'first');
  count=length(find(vector1(i)<=vector2 & vector2<vector1(i+1))); 

This turns out to be extremely slow with typical vector lengths of vector1 around 200,000 and vector2 of 1,500,000.

Any tips on ways to speed this up, or another function? Another way to look at it is vector1 is providing the bins, and vector2 has the values that fall in each bin. I want the number in each bin, and the index of the first value in each bin. I tried histc but wasn't happy with how it was working.




No products are associated with this question.

1 Answer

Answer by per isakson
on 9 Apr 2013
Edited by per isakson
on 9 Apr 2013


    doc histc


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today