Mercurial > octave-nkf
comparison scripts/control/util/sortcom.m @ 7132:b01db194c526
[project @ 2007-11-08 16:17:34 by jwe]
author | jwe |
---|---|
date | Thu, 08 Nov 2007 16:17:34 +0000 |
parents | a1dbe9d80eee |
children |
comparison
equal
deleted
inserted
replaced
7131:a184bc985c37 | 7132:b01db194c526 |
---|---|
51 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> | 51 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
52 ## Created: June 1995 | 52 ## Created: June 1995 |
53 | 53 |
54 function [yy, idx] = sortcom (xx, opt) | 54 function [yy, idx] = sortcom (xx, opt) |
55 | 55 |
56 if( nargin < 1 | nargin > 2 ) | 56 if (nargin < 1 || nargin > 2) |
57 print_usage (); | 57 print_usage (); |
58 elseif( !(isvector(xx) | isempty(xx) )) | 58 elseif (! (isvector (xx) || isempty (xx))) |
59 error("sortcom: first argument must be a vector"); | 59 error ("sortcom: first argument must be a vector"); |
60 endif | 60 endif |
61 | 61 |
62 if(nargin == 1) opt = "re"; | 62 if (nargin == 1) |
63 opt = "re"; | |
63 else | 64 else |
64 if (!ischar(opt)) | 65 if (! ischar (opt)) |
65 error("sortcom: second argument must be a string"); | 66 error ("sortcom: second argument must be a string"); |
66 endif | 67 endif |
67 endif | 68 endif |
68 | 69 |
69 if(isempty(xx)) | 70 if (isempty (xx)) |
70 yy = idx = []; | 71 yy = idx = []; |
71 else | 72 else |
72 if(strcmp(opt,"re")) datavec = real(xx); | 73 if (strcmp (opt, "re")) |
73 elseif(strcmp(opt,"im")) datavec = imag(xx); | 74 datavec = real (xx); |
74 elseif(strcmp(opt,"mag")) datavec = abs(xx); | 75 elseif (strcmp (opt, "im")) |
75 else error(["sortcom: invalid option = ", opt]) | 76 datavec = imag (xx); |
77 elseif (strcmp (opt, "mag")) | |
78 datavec = abs (xx); | |
79 else | |
80 error ("sortcom: invalid option = %s", opt); | |
76 endif | 81 endif |
77 | 82 |
78 [datavec,idx] = sort(datavec); | 83 [datavec, idx] = sort (datavec); |
79 yy= xx(idx); | 84 yy= xx(idx); |
80 | 85 |
81 if(strcmp(opt,"re") | strcmp(opt,"mag")) | 86 if (strcmp (opt, "re") || strcmp (opt, "mag")) |
82 ## sort so that complex conjugate pairs appear together | 87 ## sort so that complex conjugate pairs appear together |
83 | 88 |
84 ddiff = diff(datavec); | 89 ddiff = diff (datavec); |
85 zidx = find(ddiff == 0); | 90 zidx = find (ddiff == 0); |
86 | 91 |
87 ## sort common datavec values | 92 ## sort common datavec values |
88 if(!isempty(zidx)) | 93 if (! isempty (zidx)) |
89 for iv=create_set(datavec(zidx)) | 94 for iv = create_set (datavec(zidx)) |
90 vidx = find(datavec == iv); | 95 vidx = find (datavec == iv); |
91 [vals,imidx] = sort(imag(yy(vidx))); | 96 [vals, imidx] = sort (imag (yy(vidx))); |
92 yy(vidx) = yy(vidx(imidx)); | 97 yy(vidx) = yy(vidx(imidx)); |
93 idx(vidx) = idx(vidx(imidx)); | 98 idx(vidx) = idx(vidx(imidx)); |
94 endfor | 99 endfor |
95 endif | 100 endif |
96 endif | 101 endif |