Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Find index locations of a set of numbers relative to given vector

Subject: Find index locations of a set of numbers relative to given vector

From: Jim Rockford

Date: 11 Oct, 2010 21:41:25

Message: 1 of 6

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).

Example:

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?

Thanks,
Jim

Subject: Find index locations of a set of numbers relative to given vector

From: Joseph

Date: 11 Oct, 2010 21:52:04

Message: 2 of 6

Jim Rockford <jim.rockford1@gmail.com> wrote in message <7a380b13-9588-4d11-9d9a-72b721525c6f@t20g2000yqa.googlegroups.com>...
> 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).
>
> Example:
>
> 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?
>
> Thanks,
> Jim

If x is incremented by a constant try
x=1:increment:6;
result = sort(round(x/increment));

Then check the boundaries.

Subject: Find index locations of a set of numbers relative to given vector

From: Jos (10584)

Date: 11 Oct, 2010 21:55:06

Message: 3 of 6

Jim Rockford <jim.rockford1@gmail.com> wrote in message <7a380b13-9588-4d11-9d9a-72b721525c6f@t20g2000yqa.googlegroups.com>...
> 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).
>
> Example:
>
> 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?
>
> Thanks,
> Jim

help HISTC

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

[~, result] = histc(y,x)
result(y>x(end)) = numel(x)+1

hth
Jos

Subject: Find index locations of a set of numbers relative to given vector

From: Jim Rockford

Date: 11 Oct, 2010 22:00:43

Message: 4 of 6

On Oct 11, 5:52 pm, "Joseph " <don'twannapos...@nopers.com> wrote:
> Jim Rockford <jim.rockfo...@gmail.com> wrote in message <7a380b13-9588-4d11-9d9a-72b721525...@t20g2000yqa.googlegroups.com>...
> > 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).
>
> > Example:
>
> > 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?
>
> > Thanks,
> > Jim
>
> If x is incremented by a constant try
> x=1:increment:6;
> result = sort(round(x/increment));
>
> Then check the boundaries.


Joseph - I'm missing something in your suggestion. There is no
reference to the y vector at all (and it's these values I wish to find
the associated indices in x). You meant y instead of x in your
expression, right? As you say, all values in y either out of range
or falling precisely on a value of x would need to be checked
separately.

Subject: Find index locations of a set of numbers relative to given vector

From: Roger Stafford

Date: 12 Oct, 2010 01:26:03

Message: 5 of 6

"Jos (10584) " <#10584@fileexchange.com> wrote in message <i9013q$ldn$1@fred.mathworks.com>...
> Jim Rockford <jim.rockford1@gmail.com> wrote in message <7a380b13-9588-4d11-9d9a-72b721525c6f@t20g2000yqa.googlegroups.com>...
> > 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).
> >
> > Example:
> >
> > 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?
> >
> > Thanks,
> > Jim
>
> help HISTC
>
> x = [1 2 3 4 5 6]; % reference vector
> y = [5.8 2.4 6 -1 18 3.9];
>
> [~, result] = histc(y,x)
> result(y>x(end)) = numel(x)+1
>
> hth
> Jos
- - - - - - - - - -
  I recommend Jos' method except that the 'edges' argument array values for 'histc' should rather be set to the midpoints of the successive x values. The last value in 'edges' can be set to +inf for catching the upper out-of-range y values.

 [~,bin] = histc(y,[L,(x(1:end-1)+x(2:end))/2,U,inf]);

where L and U are the lower and upper out-of-range values. (We assume here that L is less than the least x and U is greater than the greatest x.) The 'bin' quantity gives the required index values. Note: Any y below L will automatically receive a bin number of zero as requested, and similarly any value beyond U will receive a bin value of length(x)+1.

Roger Stafford

Subject: Find index locations of a set of numbers relative to given vector

From: Jim Rockford

Date: 12 Oct, 2010 15:23:45

Message: 6 of 6

On Oct 11, 9:26 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
>   I recommend Jos' method except that the 'edges' argument array values for 'histc' should rather be set to the midpoints of the successive x values.  The last value in 'edges' can be set to +inf for catching the upper out-of-range y values.
>
>  [~,bin] = histc(y,[L,(x(1:end-1)+x(2:end))/2,U,inf]);
>
> where L and U are the lower and upper out-of-range values.  (We assume here that L is less than the least x and U is greater than the greatest x.)  The 'bin' quantity gives the required index values.  Note: Any y below L will automatically receive a bin number of zero as requested, and similarly any value beyond U will receive a bin value of length(x)+1.


Thanks guys. The key observation, of which I had not been aware
previously, is that the 2nd output argument of "histc" returns the
index of the interval in which each element falls. Doh! Previously,
I had only used "histc" for counting observations in bins.

Jim

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us