I am trying to find a routine to interpolate a 1D vector based on NEAREST NEIGHBOR. For example, if I have the following vectors, looking for the vector "d" based on the values I have in "b" but for 1 through 20.
It won't work. You always assume that x_new[i] lies between x[counter] and x[counter+1], but there is nothing to enforce that.
To take as an example, suppose x[] and y[] were the a and b vectors in your first post. Suppose then that the first value of x_new[] was 11. Then you would be looking for 11 between 1 and 5 ... and, rather uncomfortably, your code would find it! Wrongly, obviously.
There's also the possibility of exceeding array bounds with counter+1 in line 12.
Mathematically, you would remove ambiguities and discontinuities if you switched to linear interpolation rather than nearest neighbour.
EDIT: you may want to clarify what you are actually trying to do. Myself and @helios have interpreted your first post in very different ways, and I was trying to decipher it by looking at the second one.
it is 100% accurate when dealing with (drum roll) ... lines :) however you can back-solve a line, get the function, and get the answer faster, so the performance note is correct. Its not bad for line-like functions (consider x^2n away from zero). Its very limited for anything curvy, of course. Calc tells us that functions with a dx always approximate a line when you get the points close enough together, so there are merits "zoomed in" on curves also.
Thank you all very much, using your suggestions, I improved my function and checked the results with the Matlab nearest interpolation for 1D vectors and got correct results. As I mentioned previously, it might be problematic for other situations but works well for my case!