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:
Finding position in array

Subject: Finding position in array

From: Sepp

Date: 5 Jun, 2014 23:04:08

Message: 1 of 11

Hi

I have an array of real numbers, let's say e.g.

a = [1.23, 3.5, 6, 23.54, 44.234, 60.8];

The array is sorted in ascending order.

Now I have a new value, let's say 8.54 and I want to find the position in the array a. The position would be after element 6 and before element 23.54. I.e. I should somehow retrieve the index position 3 (corresponding to 6) or 4 (corresponding to 23.54).

How can I do that efficiently in Matlab? I tried out the find method but it doesn't work.

Subject: Finding position in array

From: dpb

Date: 6 Jun, 2014 04:44:51

Message: 2 of 11

On 6/5/2014 6:04 PM, Sepp wrote:
> Hi
>
> I have an array of real numbers, let's say e.g.
>
> a = [1.23, 3.5, 6, 23.54, 44.234, 60.8];
>
> The array is sorted in ascending order.
>
> Now I have a new value, let's say 8.54 and I want to find the position
> in the array a. The position would be after element 6 and before element
> 23.54. I.e. I should somehow retrieve the index position 3
> (corresponding to 6) or 4 (corresponding to 23.54).
>
> How can I do that efficiently in Matlab? I tried out the find method but
> it doesn't work.

Of course it works...

 >> a = [1.23, 3.5, 6, 23.54, 44.234, 60.8];
 >> x=8.54;
 >> find(x<a,1,'first')
ans =
      4
 >> find(x>a,1,'last')
ans =
      3
 >>

--

Subject: Finding position in array

From: Bruno Luong

Date: 6 Jun, 2014 04:50:09

Message: 3 of 11

[~, idx] = histc(8.54, a)

% Bruno

Subject: Finding position in array

From: Sepp

Date: 6 Jun, 2014 08:06:08

Message: 4 of 11

dpb <none@non.net> wrote in message <lmrh01$m1u$1@speranza.aioe.org>...
> On 6/5/2014 6:04 PM, Sepp wrote:
> > Hi
> >
> > I have an array of real numbers, let's say e.g.
> >
> > a = [1.23, 3.5, 6, 23.54, 44.234, 60.8];
> >
> > The array is sorted in ascending order.
> >
> > Now I have a new value, let's say 8.54 and I want to find the position
> > in the array a. The position would be after element 6 and before element
> > 23.54. I.e. I should somehow retrieve the index position 3
> > (corresponding to 6) or 4 (corresponding to 23.54).
> >
> > How can I do that efficiently in Matlab? I tried out the find method but
> > it doesn't work.
>
> Of course it works...
>
> >> a = [1.23, 3.5, 6, 23.54, 44.234, 60.8];
> >> x=8.54;
> >> find(x<a,1,'first')
> ans =
> 4
> >> find(x>a,1,'last')
> ans =
> 3
> >>
>
> --


Thank you very much. Both approaches works.

Subject: Finding position in array

From: dpb

Date: 6 Jun, 2014 13:47:41

Message: 5 of 11

On 6/6/2014 3:06 AM, Sepp wrote:
> dpb <none@non.net> wrote in message <lmrh01$m1u$1@speranza.aioe.org>...
>> On 6/5/2014 6:04 PM, Sepp wrote:
>> > Hi
>> >
>> > I have an array of real numbers, let's say e.g.
>> >
>> > a = [1.23, 3.5, 6, 23.54, 44.234, 60.8];
>> >
>> > The array is sorted in ascending order.
>> >
>> > Now I have a new value, let's say 8.54 and I want to find the position
>> > in the array a. The position would be after element 6 and before
>> element
>> > 23.54. I.e. I should somehow retrieve the index position 3
>> > (corresponding to 6) or 4 (corresponding to 23.54).
>> >
>> > How can I do that efficiently in Matlab? I tried out the find method
>> but
>> > it doesn't work.
>>
>> Of course it works...
>>
>> >> a = [1.23, 3.5, 6, 23.54, 44.234, 60.8];
>> >> x=8.54;
>> >> find(x<a,1,'first')
>> ans =
>> 4
>> >> find(x>a,1,'last')
>> ans =
>> 3
>> >>
>>
> Thank you very much. Both approaches works.

Yet another approach somewhat akin to Bruno's...

 >> interp1(a,1:length(a),x,'nearest')
ans =
      3
 >>

Depends on just what it is that you're after...

--

Subject: Finding position in array

From: Bruno Luong

Date: 6 Jun, 2014 17:03:12

Message: 6 of 11

>
> Yet another approach somewhat akin to Bruno's...
>
> >> interp1(a,1:length(a),x,'nearest')
> ans =
> 3

Not completely equivalent, INTERP1 will has the resulting index jumps by 1 unit when passing the middle point of the EDGES, whereas HISTC jumps right at the EDGES.

INTERP1 and HISTC methods can deal with x as vector (not FIND()).

Subject: Finding position in array

From: dpb

Date: 6 Jun, 2014 17:24:59

Message: 7 of 11

On 6/6/2014 12:03 PM, Bruno Luong wrote:
>>
>> Yet another approach somewhat akin to Bruno's...
>>
>> >> interp1(a,1:length(a),x,'nearest')
>> ans =
>> 3
>
> Not completely equivalent, INTERP1 will has the resulting index jumps by
> 1 unit when passing the middle point of the EDGES, whereas HISTC jumps
> right at the EDGES.

Didn't say it was equivalent; only that it is "_somewhat_ akin to"...

> INTERP1 and HISTC methods can deal with x as vector (not FIND()).

But FIND() returns the specific preceding/following index value
depending on the usage if that's significant. The vector of lookup
values is easily dealt with via ARRAYFUN

 >> x=[sqrt(x) x x.^1.5]
x =
     2.9223 8.5400 24.9567
 >> arrayfun(@(x) find(x>a,1,'last'),x)
ans =
      1 3 4
 >> arrayfun(@(x) find(x<a,1,'first'),x)
ans =
      2 4 5
 >>

--

Subject: Finding position in array

From: Bruno Luong

Date: 6 Jun, 2014 18:11:12

Message: 8 of 11

dpb <none@non.net> wrote in message <lmsth7$ekd$1@speranza.aioe.org>...
> On 6/6/2014 12:03 PM, Bruno Luong wrote:
> >>
> >> Yet another approach somewhat akin to Bruno's...
> >>
> >> >> interp1(a,1:length(a),x,'nearest')
> >> ans =
> >> 3
> >
> > Not completely equivalent, INTERP1 will has the resulting index jumps by
> > 1 unit when passing the middle point of the EDGES, whereas HISTC jumps
> > right at the EDGES.
>
> Didn't say it was equivalent; only that it is "_somewhat_ akin to"...

You should point out the difference, otherwise it can be missleading for OP.

>
> > INTERP1 and HISTC methods can deal with x as vector (not FIND()).
>
> But FIND() returns the specific preceding/following index value
> depending on the usage if that's significant. The vector of lookup
> values is easily dealt with via ARRAYFUN

IMO ARRAYFUN is a poor choice when speed is important for large array of x. I often prefer for-loop compared to ARRAYFUN.

Bruno

Subject: Finding position in array

From: dpb

Date: 6 Jun, 2014 18:50:15

Message: 9 of 11

On 6/6/2014 1:11 PM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <lmsth7$ekd$1@speranza.aioe.org>...
>> On 6/6/2014 12:03 PM, Bruno Luong wrote:
>> >>
>> >> Yet another approach somewhat akin to Bruno's...
>> >>
>> >> >> interp1(a,1:length(a),x,'nearest')
>> >> ans =
>> >> 3
>> >
>> > Not completely equivalent, INTERP1 will has the resulting index
>> jumps by
>> > 1 unit when passing the middle point of the EDGES, whereas HISTC jumps
>> > right at the EDGES.
>>
>> Didn't say it was equivalent; only that it is "_somewhat_ akin to"...
>
> You should point out the difference, otherwise it can be missleading for
> OP.

What's misleading? I pointed out it was an alternative; he didn't
specify precisely what solution he wished...you didn't give any
explanation of your solution, either or how it differs from FIND.

Seems pretty picky nitting to me...

>> > INTERP1 and HISTC methods can deal with x as vector (not FIND()).
>>
>> But FIND() returns the specific preceding/following index value
>> depending on the usage if that's significant. The vector of lookup
>> values is easily dealt with via ARRAYFUN
>
> IMO ARRAYFUN is a poor choice when speed is important for large array of
> x. I often prefer for-loop compared to ARRAYFUN.

Whatever...again OP didn't say anything about array size(s) from which
to judge whether speed would be an issue or not...

--

Subject: Finding position in array

From: Bruno Luong

Date: 6 Jun, 2014 19:30:11

Message: 10 of 11

dpb <none@non.net> wrote in message <lmt2h2$qso$1@speranza.aioe.org>...
> On 6/6/2014 1:11 PM, Bruno Luong wrote:

> What's misleading? I pointed out it was an alternative; he didn't
> specify precisely what solution he wished...

But he did: he wrote "The position would be after element 6 and before element 23.54."

> you didn't give any
> explanation of your solution, either or how it differs from FIND.

My solution is the same as FIND(..., 'first'), there might be a confusion about < and <=, but I guess OP can read the DOC to figure out the detail.

However INTERP1 is the only proposed solution that does not accomplish what OP has specified, since gives a position before (6+23.54)/2. Therefore it is misleading.
 
>
> Whatever...again OP didn't say anything about array size(s) from which
> to judge whether speed would be an issue or not...

I claim it's a poor choice using ARRAYFUN in general. Just a litle bit like EVAL is a poor choice in general (OK, may be not that problematic), but IMO new users should learn NOT to abuse the usage of ARRAYFUN.

Bruno
Bruno

Subject: Finding position in array

From: dpb

Date: 6 Jun, 2014 22:42:35

Message: 11 of 11

On 6/6/2014 2:30 PM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <lmt2h2$qso$1@speranza.aioe.org>...
>> On 6/6/2014 1:11 PM, Bruno Luong wrote:
>
>> What's misleading? I pointed out it was an alternative; he didn't
>> specify precisely what solution he wished...
>
> But he did: he wrote "The position would be after element 6 and before
> element 23.54."
>
...

OK, I see I whiffed on that 'un; I read it as "or" instead of the two
values given--my bad there. In that case I'd have only given the one
FIND solution.

--

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