title: MATLAB: search for elements in an array matching multidimensional condition
body: <p>I have a column vector (V1) of real numbers like:</p>
  
  <pre><code>123.2100
  125.1290
  ...
  954.2190
  </code></pre>
  
  <p>If I add, let's say, a number 1 to each row in this vector, I will get (V2):</p>
  
  <pre><code>124.2100
  126.1290
  ...
  955.2190
  </code></pre>
  
  <p>I need to find out how many elements from V2 are inside some error-window created from V1. For example the error-window = 0.1 (but in my case every element in V1 has it's own error window):</p>
  
  <pre><code>123.1100 123.3100
  125.0290 125.2290
  ...
  954.1190 954.3190
  </code></pre>
  
  <p>I can create some code like this:</p>
  
  <pre><code>% x - my vector
  % ppm - a variable responsible for error-window
  window = [(1-(ppm/1000000))*x, (1+(ppm/1000000))*x]; % - error-window
  mdiff = 1:0.001:20; % the numbers I will iteratively add to x 
                      % (like the number 1 in the example)
  cdiff = zeros(length(mdiff),1); % a vector that will contain counts of elements
                                  % corresponding to different mdiff temp = 0;
  
  for i = 1:length(mdiff)
      for j = 1:size(window,1)
          xx = x + mdiff(i);
          indxx = find( xx =&gt; window(j,1) &amp; xx &lt;= window(j,2) );
          if any(indxx)
              temp = temp + length(indxx); %edited
          end
      end
      cdiff(i) = temp;
      temp = 0;
  end
  </code></pre>
  
  <p>So, at the end cdiff will contain all the counts corresponding to mdiff. The only thing, I would like to make the code faster. Or is there a way to avoid using the second loop (with j)? I mean to directly use a multidimensional condition.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I decided to simpify the code like this (thanking to the feedback I got here):</p>
  
  <pre><code>% x - my vector
  % ppm - a variable responsible for error-window
  window = [(1-(ppm/1000000))*x, (1+(ppm/1000000))*x]; % - error-window
  mdiff = 1:0.001:20; % the numbers I will iteratively add to x 
                      % (like the number 1 in the example)
  cdiff = zeros(length(mdiff),1); % a vector that will contain counts of elements
                                  % corresponding to different mdiff temp = 0;
  
  for i = 1:length(mdiff)
      xx = x + mdiff(i);
      cdiff(i) = sum(sum(bsxfun(@and,bsxfun(@ge,xx,window(:,1)'),bsxfun(@le,xx,window(:,2)'))));
  end
  </code></pre>
  
  <p>In this case the code works faster and seems properly</p>