From: Jim Rockford <>
Newsgroups: comp.soft-sys.matlab
Subject: Find index locations of a set of numbers relative to given vector
Date: Mon, 11 Oct 2010 14:41:25 -0700 (PDT)
Lines: 23
Message-ID: <>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: 1286833285 32137 (11 Oct 2010 21:41:25 GMT)
NNTP-Posting-Date: Mon, 11 Oct 2010 21:41:25 +0000 (UTC)
Injection-Info:; posting-host=; posting-account=4Urz8AoAAACdZdE7V_yvznqNgM-aThfn
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
 Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729),gzip(gfe)
Xref: comp.soft-sys.matlab:677536

This is easy to do in a for-loop, and what I'm asking is whether there
is something quicker.  Given a sorted (increasing values) reference
vector  x, and another vector y containing a bunch of random values,
then identify for each element of y the index of the element of x to
which each value of y is the closest.  If any y value is out of the
range of x then return  0  if  the y value is too small,  and  return
length(x)+1  if it is too large.  (for those who know numerical
recipes this is what the "locate" recipe does).


x = [1  2  3  4  5  6];  % reference vector
y = [5.8  2.4  6  -1  18  3.9];

The y-value 5.8  falls in the 5th interval of x, so the function would
return 5 for this value.  Etc.

result = [5  2  6  0  7  3];

Any functions out there that handle this?