annotate scripts/general/sortrows.m @ 33623:4517f929c59d bytecode-interpreter tip

maint: Merge default to bytecode-interpreter
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 24 May 2024 16:22:41 -0400
parents c6ef7981b6f1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 2000-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
7 ##
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
8 ## This file is part of Octave.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
11 ## 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
12 ## 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
13 ## (at your option) any later version.
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
14 ##
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
19 ##
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5443
diff changeset
21 ## 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
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
25
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
27 ## @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
28 ## @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
29 ## 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
30 ## specified in @var{c}.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ##
21675
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
32 ## 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
33 ## 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
34 ## 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
35 ## 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
36 ##
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
37 ## 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
38 ##
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
39 ## @example
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
40 ## @group
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
41 ## x = [ 7, 1, 4;
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
42 ## 8, 3, 5;
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
43 ## 9, 3, 6 ];
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
44 ## sortrows (x, [-2, 3])
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
45 ## @result{} 8 3 5
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
46 ## 9 3 6
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
47 ## 7 1 4
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
48 ## @end group
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
49 ## @end example
e47eb34f1b05 sortrows.m: Improve docstring (bug #47844).
Rik <rik@octave.org>
parents: 20158
diff changeset
50 ##
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
51 ## @seealso{sort}
5182
5b361aa47dff [project @ 2005-03-03 06:21:47 by jwe]
jwe
parents: 5181
diff changeset
52 ## @end deftypefn
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
53
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
54 function [s, i] = sortrows (A, c)
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
55
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27978
diff changeset
56 if (nargin < 1)
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
57 print_usage ();
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
58 endif
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
59
13293
007ebc128ab5 fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents: 13291
diff changeset
60 if (nargin == 2)
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
61 if (! (isnumeric (c) && isvector (c)))
13293
007ebc128ab5 fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents: 13291
diff changeset
62 error ("sortrows: C must be a numeric vector");
007ebc128ab5 fix input validation for sortrows
John W. Eaton <jwe@octave.org>
parents: 13291
diff changeset
63 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
64 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
65 endif
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
66 endif
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
67
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
68 default_mode = "ascend";
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
69 reverse_mode = "descend";
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
70
26265
01f1e70c80b6 sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 25054
diff changeset
71 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
72 ## 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
73 ## 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
74 if (nargin == 1)
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
75 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
76 else
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
77 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
78 endif
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
79 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
80 i = __sort_rows_idx__ (A, default_mode);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
81 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
82 i = __sort_rows_idx__ (A(:,c), default_mode);
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8455
diff changeset
83 elseif (all (c < 0))
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
84 i = __sort_rows_idx__ (A(:,-c), reverse_mode);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
85 else
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
86 ## 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
87 i = sort_rows_idx_generic (default_mode, reverse_mode, A, c);
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
88 endif
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
89
32786
c6ef7981b6f1 replace uses of isargout with checks for nargout
John W. Eaton <jwe@octave.org>
parents: 32632
diff changeset
90 s = A(i,:);
5181
41cd70503c72 [project @ 2005-03-03 05:49:55 by jwe]
jwe
parents: 5178
diff changeset
91
5178
6758c11b5b99 [project @ 2005-03-03 05:18:04 by jwe]
jwe
parents:
diff changeset
92 endfunction
7678
5988e08c1ae6 handle negative column values in sortrows
Richard Bovey
parents: 7017
diff changeset
93
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
94 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
95
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
96 if (nargin == 3)
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
97 indices = [1:columns(m)]';
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
98 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
99 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
100 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
101 if (c(j) < 0)
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
102 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
103 else
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
104 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
105 endif
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
106 endfor
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
107 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
108 endif
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
109
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
110 ## 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
111 ## 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
112 ## 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
113 ## index j.
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
114 indices = flipud (indices);
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
115 mode = flipud (mode');
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
116 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
117 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
118 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
119 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
120 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
121 endif
01f1e70c80b6 sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 25054
diff changeset
122 [~, 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
123 i = i(idx);
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
124 endfor
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
125
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
126 endfunction
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
127
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
128
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10850
diff changeset
129 %!test
9669
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
130 %! 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
131 %! c = [1, -2];
63249224f78d sortrows: also fall back on old algorithm for sparse matrices
John W. Eaton <jwe@octave.org>
parents: 9041
diff changeset
132 %! [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
133 %! [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
134 %! 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
135 %! 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
136 %! 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
137 %! 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
138 %! 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
139
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
140 %!test
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
141 %! 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
142 %! c = 1;
e38fb1910563 Allow sortrows to work on arrays with one row (bug #33197)
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
143 %! [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
144 %! [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
145 %! 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
146 %! 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
147 %! 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
148 %! 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
149 %! assert (idx, sidx);
13291
6cebb0c36b1d sortrows.m: Improve input validation and add more tests.
Rik <octave@nomad.inbox5.com>
parents: 12647
diff changeset
150
26265
01f1e70c80b6 sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523).
Guillaume Flandin <guillaume.offline@gmail.com>
parents: 25054
diff changeset
151 %!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
152 %! 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
153 %! 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
154 %! 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
155 %! 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
156 %! 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
157
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
158 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28789
diff changeset
159 %!error <Invalid call> sortrows ()
13291
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)