Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Finding position in array
Replies: 10   Last Post: Jun 6, 2014 6:42 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Helveticus

Posts: 10
Registered: 11/3/13
Re: Finding position in array
Posted: Jun 6, 2014 4:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.