title: Faster alternative to find() in MATLAB
body: <p>I'm trying to write (an efficient) MATLAB code that does the following:</p>
  
  <p>I have ~100,000 2-D data-points, and I have pairs of intervals. The first interval doesn't change (in this example between 0 and 1), and the second interval keeps changing.<br>
  I want to get the instances/data-points that have:</p>
  
  <p>1) x-coordinate values within the first interval (0,1)<br>
  2) y-coordinate values within the second (changing interval)</p>
  
  <pre><code>% firstCol is a ~100,000 rows, one column array; x-coordinate
  % secondCol is also a ~100,000 rows, one column array; y-coordinate
  
  % boundaries of my first interval
  %
  maxOfMyFirstInterval = 1;
  minOfMyFirstInterval = 0;
  
  % allIntervalsMax is a ~10,000 rows, one column, of maximum values
  % allIntervalsMin is a ~10,000 rows, one column, of minimum values
  % 
  % The above two columns contain the changing pairs, so the first pair would be
  % (allIntervalsMin(1), allIntervalsMax(1))
  %
  
  % pre-allocate array that will hold number of data-points that satisfy 
  % my condition
  %
  numberOfInstances = zeros(length(allIntervalsMax),1);
  
  tic
  
  % This will get the instances that satisfy my first condition,
  % x-coordinate between 0 and 1
  %
  a_first = find((firstCol &lt;= maxOfMyFirstInterval) &amp; ...
      (firstCol &gt;= minOfMyFirstInterval));
  
  % Loop through the list of second intervals
  %
  for jx = 1:length(allIntervalsMax)
  
      a_second = find((secondCol &lt;= allIntervalsMax(jx)) &amp; ...
          (secondCol &gt;= allIntervalsMin(jx)));
  
      a_both = intersect(a_first, a_second);
  
      numberOfInstances(jx) = length(a_both);
  end
  
  toc
  </code></pre>
  
  <p>The time it takes to do that is ~29 seconds, I was wondering if there's a faster way.</p>