3431
|
1 ## Copyright (C) 1996 Auburn University. All rights reserved. |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
7016
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3431
|
9 ## |
7016
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
3431
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3431
|
18 |
|
19 ## -*- texinfo -*- |
5016
|
20 ## @deftypefn {Function File} {[@var{yy}, @var{idx}] =} sortcom (@var{xx}[, @var{opt}]) |
|
21 ## Sort a complex vector. |
3431
|
22 ## |
5016
|
23 ## @strong{Inputs} |
|
24 ## @table @var |
|
25 ## @item xx |
|
26 ## Complex vector |
|
27 ## @item opt |
|
28 ## sorting option: |
|
29 ## @table @code |
|
30 ## @item "re" |
|
31 ## Real part (default); |
|
32 ## @item "mag" |
|
33 ## By magnitude; |
|
34 ## @item "im" |
|
35 ## By imaginary part. |
|
36 ## @end table |
|
37 ## if @var{opt} is not chosen as @code{"im"}, then complex conjugate pairs are grouped together, |
|
38 ## @math{a - jb} followed by @math{a + jb}. |
|
39 ## @end table |
|
40 ## |
|
41 ## @strong{Outputs} |
|
42 ## @table @var |
|
43 ## @item yy |
|
44 ## Sorted values |
|
45 ## @item idx |
|
46 ## Permutation vector: @code{yy = xx(idx)} |
|
47 ## @end table |
3431
|
48 ## @end deftypefn |
|
49 |
|
50 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
51 ## Created: June 1995 |
|
52 |
|
53 function [yy, idx] = sortcom (xx, opt) |
|
54 |
|
55 if( nargin < 1 | nargin > 2 ) |
6046
|
56 print_usage (); |
4030
|
57 elseif( !(isvector(xx) | isempty(xx) )) |
3431
|
58 error("sortcom: first argument must be a vector"); |
|
59 endif |
|
60 |
|
61 if(nargin == 1) opt = "re"; |
|
62 else |
5443
|
63 if (!ischar(opt)) |
3431
|
64 error("sortcom: second argument must be a string"); |
|
65 endif |
|
66 endif |
|
67 |
|
68 if(isempty(xx)) |
|
69 yy = idx = []; |
|
70 else |
|
71 if(strcmp(opt,"re")) datavec = real(xx); |
|
72 elseif(strcmp(opt,"im")) datavec = imag(xx); |
|
73 elseif(strcmp(opt,"mag")) datavec = abs(xx); |
|
74 else error(["sortcom: invalid option = ", opt]) |
|
75 endif |
|
76 |
|
77 [datavec,idx] = sort(datavec); |
|
78 yy= xx(idx); |
|
79 |
|
80 if(strcmp(opt,"re") | strcmp(opt,"mag")) |
|
81 ## sort so that complex conjugate pairs appear together |
|
82 |
|
83 ddiff = diff(datavec); |
|
84 zidx = find(ddiff == 0); |
|
85 |
|
86 ## sort common datavec values |
|
87 if(!isempty(zidx)) |
|
88 for iv=create_set(datavec(zidx)) |
|
89 vidx = find(datavec == iv); |
|
90 [vals,imidx] = sort(imag(yy(vidx))); |
|
91 yy(vidx) = yy(vidx(imidx)); |
|
92 idx(vidx) = idx(vidx(imidx)); |
|
93 endfor |
|
94 endif |
|
95 endif |
|
96 endif |
|
97 endfunction |
|
98 |