help-octave
[Top][All Lists]

## RE: sort question

 From: Michael Underwood Subject: RE: sort question Date: Wed, 5 May 2004 17:20:06 +1000

```I probably didn't explain properly.
ib gives the permutation vector (e.g. in my example, it would tell me that the
1st element of b is the 3rd element of a). I'm basically looking for the
inverse; for example, what element of b does a(1) equal.
This could be done using
b = sort(a);
for k = 1 to length(a),
ic(k) = find(b==a(k));
endfor
but I am doing this on really large 2-D arrays

-----Original Message-----
Sent: Wednesday, 5 May 2004 5:05 pm
Subject: Re: sort question

Easy: ask two return values of sort ():

octave> [b, ib] = sort (a)

The second holds the desired indices.

On Wed, 5 May 2004 04:56 pm, Michael Underwood wrote:
> I looking for a neat way of determining the index that of each element of
> an array would have if sorted.
>
> eg.
> octave:1> a = rand(4,1)
> a =
>
>   0.73433
>   0.55564
>   0.49191
>   0.90766
>
> octave:2> b= sort(a)
> b =
>
>   0.49191
>   0.55564
>   0.73433
>   0.90766
>
> for instance, a(1)  is the 3rd element of b.
>
> I know this can be done with the find command, but the array I am dealing
> with are sufficiently large to make the find command too time consuming.
>
> Any ideas??
>
>
> Regards
>
>       Michael Underwood
>
>       Systems Analyst
> Generation Division
> Hydro Tasmania
>
>       Ph: (03) 6230 5494
>
>       This message and any attachments may contain confidential or privileged
> information, and are intended solely for the named recipient(s). If you are
> not a named recipient of this message, you are hereby notified that you
> must not use, disseminate, copy or take any action in reliance on this
> message or any part of it. If you have received this message in error,
> please notify the author immediately via
>
>       NB: Unless otherwise specified, opinions and personal views expressed
> herein are purely those of the author and do not represent the views of
> Hydro Tasmania.

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```