title: how do I find the closest value to a given number in an array?
body: <p>The Situation is as follows:</p>
  
  <p>I have to arrays, symbolizing a positive domain <code>x</code>, and another array whose a function of that domain <code>z</code></p>
  
  <p>Now, I want, for a given point <code>y</code>, to find the <code>z</code> value in the nearest location. For thatI wrote the following function:</p>
  
  <pre><code>R0 = @(y) z(find(abs( abs(y). -  r) == min(abs(abs(y). - r))))
  </code></pre>
  
  <p>(The use of <code>abs</code> is for negative values of <code>y</code>, since <code>z</code> is symmetric)</p>
  
  <p>This works perfectally well, unless <code>y</code> is a vector. So, if I use the following code:</p>
  
  <pre><code>y = [-1:0.01:1];
  R0(y);
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Error using  == 
  Matrix dimensions must agree.
  </code></pre>
  
  <p>Trying to debug it, I came to see that the <code>find</code> statement returned a 1*0 matrix, hence nothing. This is although the value of <code>y</code> ACTUALLY EXSIST in the <code>r</code> array.</p>
  
  <p>What I really want is to get a new vector, which assign the nearest value in <code>z</code> for each value of <code>y</code>.</p>
  
  <p>Other, totally different solutions might be used, so I prefer understanding why this solution doesn't work and how can I make it work.</p>
  
  <p>Thanks</p>