title: matlab histc with discontinuous intervals
body: <p>I am stuck with this problem for the past few days! So, any help would be greatly appreciated.</p>
  
  <p>THE PROBLEM:</p>
  
  <hr>
  
  <p>I have a huge list of discontinuous intervals, about 80000 of them, spread over the range from 1 to 250 million. And I have a much larger set of numbers (a vector of about 200 million elements) spread over the same range, i.e., 1 to 250 million. As if this was not complex enough, some of these intervals may overlap, but most of them are expected to be discontinuous.</p>
  
  <p>Now I want to do a histogram count of how many elements of the vector fall within each of the intervals. If these were simply continuous intervals (without gaps), then one could use histc. Even if the intervals were guaranteed to be always discontinuous (i.e., with gaps), I could have still used histc and thrown out the counts in the gaps. But since I do not have the guarantee that the intervals would always be discontinuous, I am stumped.</p>
  
  <p>So far, I tried two ways of attacking this problem:</p>
  
  <p>1) Simply loop through the intervals, and do sum(vector >= start &amp; vector &lt;= end) inside the for loop. This was hopelessly slow.</p>
  
  <p>2) Use cellfun or arrayfun like this: cellfun(@(L,U) sum(histc(vector,[L U])), Strts, Ends); where Strts and Ends are cell arrays defining the intervals.</p>
  
  <p>Although the second solution would take a few hours, since I need to do this operation for hundreds of large datasets, I cannot afford the time.</p>
  
  <p>So, is there a better way?</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>Thanks!</p>