help-octave
[Top][All Lists]

## Re: sort question

 From: Geraint Paul Bevan Subject: Re: sort question Date: Wed, 05 May 2004 08:37:51 +0100 User-agent: Mozilla Thunderbird 0.5 (X11/20040306)

```-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael Underwood wrote:
| 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
|

Is this what you are after?

octave:1> a = rand(4,1);
octave:2> [b,ib] = sort(a)
b =

~  0.017655
~  0.081125
~  0.197712
~  0.790007

ib =

~  2
~  4
~  1
~  3

octave:3> c(ib) = [1:4]
c =

~  3  1  4  2

- --
Geraint Bevan
http://homepage.ntlworld.com/geraint.bevan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iEYEARECAAYFAkCYmc4ACgkQcXV3N50QmNNLEwCcCoLfHCtHPwM0LX5HoOqVvmKy
CsYAn35OQE8Zkm0bgmEvSi/k04yPjuIr
=Dd8m
-----END PGP SIGNATURE-----

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

```