How do I find values close to a given value?

nsy picture nsy · Nov 11, 2011 · Viewed 31.9k times · Source

I have data = [1 1.2 1.3 1.5 1.8]

I want to find closest values before and after from data for this point, b = 1.23

How do I do this?

Answer

Doubt picture Doubt · Jun 6, 2013

Here is another method. The vector data need not be sorted and b can be positive or negative.

[~,I] = min(abs(data-b));
c = data(I);