Mercurial > octave
annotate scripts/general/sortrows.m @ 27898:4d6d21839dfd
remove Author: jwe and Adapted-by: jwe lines from source files
These lines have little meaning now in the age of modern version
control systems.
Files affected: url-transfer.cc, url-transfer.h, acot.m, acoth.m,
acsc.m, acsch.m, asec.m, asech.m, cot.m, coth.m, csc.m, csch.m, sec.m,
sech.m, cart2pol.m, cart2sph.m, deal.m, fliplr.m, flipud.m, int2str.m,
logspace.m, num2str.m, pol2cart.m, rot90.m, sortrows.m, sph2cart.m,
waitbar.m, doc.m, hsv2rgb.m, rgb2hsv.m, beep.m, isstr.m, setstr.m,
strmatch.m, cond.m, rank.m, trace.m, bug_report.m, cast.m, delete.m,
dir.m, list_primes.m, ls.m, ls_command.m, menu.m, pack.m, recycle.m,
substruct.m, version.m, glpk.m, axis.m, box.m, grid.m,
__axis_label__.m, text.m, title.m, view.m, xlabel.m, ylabel.m,
zlabel.m, bar.m, barh.m, comet3.m, hist.m, line.m, loglog.m, mesh.m,
patch.m, plot.m, polar.m, __bar__.m, __line__.m, __plt__.m,
__stem__.m, semilogx.m, semilogy.m, sombrero.m, stairs.m, stem.m,
surface.m, __default_plot_options__.m, __gnuplot_drawnow__.m,
__next_line_color__.m, __plt_get_axis_arg__.m, __pltopt__.m, clf.m,
closereq.m, gca.m, gnuplot_binary.in.m, isfigure.m, meshgrid.m,
__gnuplot_draw_axes__.m, __gnuplot_draw_figure__.m, shg.m, addpref.m,
getpref.m, ispref.m, loadprefs.m, prefsfile.m, saveprefs.m, rmpref.m,
setpref.m, ismember.m, setdiff.m, union.m, hankel.m, hilb.m, vander.m,
median.m, std.m, cstrcat.m, isletter.m, str2num.m, strcat.m,
oruntests.m, rundemos.m, asctime.m, clock.m, ctime.m, date.m, etime.m,
is_leap_year.m.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 02 Jan 2020 15:43:01 -0500 |
parents | 00f796120a6d |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
26268
diff
changeset
|
1 ## Copyright (C) 2000-2019 Daniel Calvelo |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
2 ## Copyright (C) 2009 Jaroslav Hajek |
5178 | 3 ## |
5181 | 4 ## This file is part of Octave. |
5178 | 5 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
6 ## Octave is free software: you can redistribute it and/or modify it |
5181 | 7 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
8 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
9 ## (at your option) any later version. |
5181 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
14 ## GNU General Public License for more details. |
5178 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
18 ## <https://www.gnu.org/licenses/>. |
5178 | 19 |
5182 | 20 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
21 ## @deftypefn {} {[@var{s}, @var{i}] =} sortrows (@var{A}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
22 ## @deftypefnx {} {[@var{s}, @var{i}] =} sortrows (@var{A}, @var{c}) |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
23 ## Sort the rows of the matrix @var{A} according to the order of the columns |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
24 ## specified in @var{c}. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
25 ## |
21675
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
26 ## By default (@var{c} omitted, or a particular column unspecified in @var{c}) |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
27 ## an ascending sort order is used. However, if elements of @var{c} are |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
28 ## negative then the corresponding column is sorted in descending order. If |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
29 ## the elements of @var{A} are strings then a lexicographical sort is used. |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
30 ## |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
31 ## Example: sort by column 2 in descending order, then 3 in ascending order |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
32 ## |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
33 ## @example |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
34 ## @group |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
35 ## x = [ 7, 1, 4; |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
36 ## 8, 3, 5; |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
37 ## 9, 3, 6 ]; |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
38 ## sortrows (x, [-2, 3]) |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
39 ## @result{} 8 3 5 |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
40 ## 9 3 6 |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
41 ## 7 1 4 |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
42 ## @end group |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
43 ## @end example |
e47eb34f1b05
sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
44 ## |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
45 ## @seealso{sort} |
5182 | 46 ## @end deftypefn |
5178 | 47 |
5181 | 48 ## Author: Daniel Calvelo, Paul Kienzle |
5178 | 49 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
50 function [s, i] = sortrows (A, c) |
7678 | 51 |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
52 if (nargin < 1 || nargin > 2) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
53 print_usage (); |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
54 endif |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
55 |
13293
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
56 if (nargin == 2) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
57 if (! (isnumeric (c) && isvector (c))) |
13293
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
58 error ("sortrows: C must be a numeric vector"); |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
59 elseif (any (c == 0) || any (abs (c) > columns (A))) |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
60 error ("sortrows: all elements of C must be in the range [1, columns (A)]"); |
007ebc128ab5
fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents:
13291
diff
changeset
|
61 endif |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
62 endif |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
63 |
7678 | 64 default_mode = "ascend"; |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
65 reverse_mode = "descend"; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
66 |
26265
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
67 if (issparse (A) || iscell (A)) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
68 ## FIXME: Eliminate this case once __sort_rows_idx__ is fixed to |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
69 ## handle sparse matrices. |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
70 if (nargin == 1) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
71 i = sort_rows_idx_generic (default_mode, reverse_mode, A); |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
72 else |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
73 i = sort_rows_idx_generic (default_mode, reverse_mode, A, c); |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
74 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
75 elseif (nargin == 1) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
76 i = __sort_rows_idx__ (A, default_mode); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
77 elseif (all (c > 0)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
78 i = __sort_rows_idx__ (A(:,c), default_mode); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
79 elseif (all (c < 0)) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
80 i = __sort_rows_idx__ (A(:,-c), reverse_mode); |
5178 | 81 else |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
82 ## Otherwise, fall back to the old algorithm. |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
83 i = sort_rows_idx_generic (default_mode, reverse_mode, A, c); |
5178 | 84 endif |
85 | |
10850
6c57bd7d0808
isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
86 ## Only bother to compute s if needed. |
6c57bd7d0808
isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
87 if (isargout (1)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
88 s = A(i,:); |
10850
6c57bd7d0808
isargout optimization in sortrows
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
89 endif |
5181 | 90 |
5178 | 91 endfunction |
7678 | 92 |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
93 function i = sort_rows_idx_generic (default_mode, reverse_mode, m, c) |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
94 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
95 if (nargin == 3) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
96 indices = [1:columns(m)]'; |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
97 mode(1:columns(m)) = {default_mode}; |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
98 else |
26265
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
99 for j = 1:length (c) |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
100 if (c(j) < 0) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
101 mode{j} = reverse_mode; |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
102 else |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
103 mode{j} = default_mode; |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
104 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
105 endfor |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
106 indices = abs (c(:)); |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
107 endif |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
108 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
109 ## Since sort is 'stable' the order of identical elements will be |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
110 ## preserved, so by traversing the sort indices in reverse order we |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
111 ## will make sure that identical elements in index i are subsorted by |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
112 ## index j. |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
113 indices = flipud (indices); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
114 mode = flipud (mode'); |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
115 i = [1:rows(m)]'; |
26268
6dd232798997
maint: Remove useless ';' from end of for, if, while, etc. statements.
Rik <rik@octave.org>
parents:
26265
diff
changeset
|
116 for j = 1:length (indices) |
26265
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
117 M = m(i, indices(j)); |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
118 if (iscell (M) && ! iscellstr (M)) |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
119 M = cell2mat (M); |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
120 endif |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
121 [~, idx] = sort (M, mode{j}); |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
122 i = i(idx); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
123 endfor |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
124 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
125 endfunction |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
126 |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
127 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
128 %!test |
9669
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
129 %! m = [1, 1; 1, 2; 3, 6; 2, 7]; |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
130 %! c = [1, -2]; |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
131 %! [x, idx] = sortrows (m, c); |
63249224f78d
sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9041
diff
changeset
|
132 %! [sx, sidx] = sortrows (sparse (m), c); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
133 %! assert (x, [1, 2; 1, 1; 2, 7; 3, 6]); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
134 %! assert (idx, [2; 1; 4; 3]); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
135 %! assert (issparse (sx)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
136 %! assert (x, full (sx)); |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10850
diff
changeset
|
137 %! assert (idx, sidx); |
12647
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
138 |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
139 %!test |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
140 %! m = [1, 0, 0, 4]; |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
141 %! c = 1; |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
142 %! [x, idx] = sortrows (m, c); |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
143 %! [sx, sidx] = sortrows (sparse (m), c); |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
144 %! assert (x, m); |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
145 %! assert (idx, 1); |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
146 %! assert (issparse (sx)); |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
147 %! assert (x, full (sx)); |
e38fb1910563
Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
148 %! assert (idx, sidx); |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
149 |
26265
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
150 %!test <*42523> |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
151 %! C = {1, 2, "filename1"; |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
152 %! 3, 4, "filename2"; |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
153 %! 5, 6, "filename3"}; |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
154 %! C2 = sortrows (C, -1); |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
155 %! assert (C2, flipud (C)); |
01f1e70c80b6
sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
25054
diff
changeset
|
156 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
157 ## Test input validation |
13291
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
158 %!error sortrows () |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
159 %!error sortrows (1, 2, 3) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
160 %!error sortrows (1, "ascend") |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
161 %!error sortrows (1, ones (2,2)) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
162 %!error sortrows (1, 0) |
6cebb0c36b1d
sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents:
12647
diff
changeset
|
163 %!error sortrows (1, 2) |