]> Creatis software - CreaPhase.git/blob - octave_packages/fixed-0.7.10/fsort.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / fixed-0.7.10 / fsort.m
1 ## Copyright (C) 2003  Motorola Inc
2 ## Copyright (C) 2003  David Bateman
3 ##
4 ## This program is free software; you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2 of the License, or
7 ## (at your option) any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {[@var{s}, @var{i}] =} fsort (@var{x})
19 ## Return a copy of the fixed point variable @var{x} with the elements
20 ## arranged in increasing order.  For matrices, @code{fsort} orders the 
21 ## elements in each column.
22 ##
23 ## For example,
24 ##
25 ## @example
26 ## @group
27 ## fsort (fixed(4,0,[1, 2; 2, 3; 3, 1]))
28 ##     @result{}  1  1
29 ##         2  2
30 ##         3  3
31 ## @end group
32 ## @end example
33 ##
34 ## The @code{fsort} function may also be used to produce a matrix
35 ## containing the original row indices of the elements in the sorted
36 ## matrix.  For example,
37 ##
38 ## @example
39 ## @group
40 ## [s, i] = sort ([1, 2; 2, 3; 3, 1])
41 ##      @result{} s = 1  1
42 ##             2  2
43 ##             3  3
44 ##      @result{} i = 1  3
45 ##             2  1
46 ##             3  2
47 ## @end group
48 ## @end example
49 ## @end deftypefn
50
51 ## PKG_ADD: dispatch ("sort", "fsort", "fixed scalar")
52 ## PKG_ADD: dispatch ("sort", "fsort", "fixed matrix")
53 ## PKG_ADD: dispatch ("sort", "fsort", "fixed complex")
54 ## PKG_ADD: dispatch ("sort", "fsort", "fixed complex matrix")
55
56 function [s, i] = fsort (x)
57    if (!isfixed(x))
58      error("fsort: input argument not of fixed point type");
59    endif
60    [a, i] = sort(x.x);
61    if isvector(a)
62      s = x(i);
63    else
64      s = x;
65      for j=1:size(i,2)
66        s(:,j) = s(i(:,j),j);
67      endfor
68    endif
69 endfunction