2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
4153
|
28 #include "quit.h" |
|
29 |
2928
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 |
4678
|
35 inline double real (double x) { return x; } |
|
36 |
|
37 template <typename T> |
|
38 octave_value_list |
|
39 find_nonzero_elem_idx (const T& nda, int nargout) |
|
40 { |
|
41 octave_value_list retval (((nargout == 0) ? 1 : nargout), Matrix ()); |
|
42 |
5275
|
43 octave_idx_type count = 0; |
4678
|
44 |
5275
|
45 octave_idx_type nel = nda.nelem (); |
4678
|
46 |
5275
|
47 for (octave_idx_type i = 0; i < nel; i++) |
4678
|
48 { |
|
49 OCTAVE_QUIT; |
|
50 |
|
51 if (nda(i) != 0.0) |
|
52 count++; |
|
53 } |
|
54 |
4826
|
55 // If the original argument was a row vector, force a row vector of |
5130
|
56 // the overall indices to be returned. But see below for scalar |
|
57 // case... |
4678
|
58 |
5275
|
59 octave_idx_type result_nr = count; |
|
60 octave_idx_type result_nc = 1; |
4678
|
61 |
5130
|
62 bool scalar_arg = false; |
|
63 |
4826
|
64 if (nda.ndims () == 2 && nda.rows () == 1) |
|
65 { |
|
66 result_nr = 1; |
|
67 result_nc = count; |
5130
|
68 |
|
69 scalar_arg = (nda.columns () == 1); |
4826
|
70 } |
3418
|
71 |
4826
|
72 Matrix idx (result_nr, result_nc); |
4678
|
73 |
4826
|
74 Matrix i_idx (result_nr, result_nc); |
|
75 Matrix j_idx (result_nr, result_nc); |
4678
|
76 |
4826
|
77 T val (dim_vector (result_nr, result_nc)); |
|
78 |
|
79 if (count > 0) |
4678
|
80 { |
4826
|
81 count = 0; |
|
82 |
5275
|
83 octave_idx_type nr = nda.rows (); |
4678
|
84 |
5275
|
85 octave_idx_type i = 0; |
|
86 octave_idx_type j = 0; |
4826
|
87 |
5275
|
88 for (octave_idx_type k = 0; k < nel; k++) |
4678
|
89 { |
4826
|
90 OCTAVE_QUIT; |
4678
|
91 |
4826
|
92 if (nda(k) != 0.0) |
|
93 { |
|
94 idx(count) = k + 1; |
4678
|
95 |
4826
|
96 i_idx(count) = i + 1; |
|
97 j_idx(count) = j + 1; |
4678
|
98 |
4826
|
99 val(count) = nda(k); |
|
100 |
|
101 count++; |
|
102 } |
4678
|
103 |
4826
|
104 i++; |
4678
|
105 |
4826
|
106 if (i == nr) |
|
107 { |
|
108 i = 0; |
|
109 |
|
110 j++; |
|
111 } |
4678
|
112 } |
|
113 } |
5130
|
114 else if (scalar_arg) |
5131
|
115 { |
|
116 idx.resize (0, 0); |
|
117 |
|
118 i_idx.resize (0, 0); |
|
119 j_idx.resize (0, 0); |
|
120 |
|
121 val.resize (dim_vector (0, 0)); |
|
122 } |
2928
|
123 |
|
124 switch (nargout) |
|
125 { |
|
126 case 0: |
|
127 case 1: |
4826
|
128 retval(0) = idx; |
2928
|
129 break; |
|
130 |
|
131 case 3: |
|
132 retval(2) = val; |
|
133 // Fall through! |
|
134 |
|
135 case 2: |
3418
|
136 retval(1) = j_idx; |
|
137 retval(0) = i_idx; |
2928
|
138 break; |
|
139 |
|
140 default: |
|
141 panic_impossible (); |
|
142 break; |
|
143 } |
|
144 |
|
145 return retval; |
|
146 } |
|
147 |
4678
|
148 template octave_value_list find_nonzero_elem_idx (const NDArray&, int); |
2928
|
149 |
4678
|
150 template octave_value_list find_nonzero_elem_idx (const ComplexNDArray&, int); |
2928
|
151 |
|
152 DEFUN_DLD (find, args, nargout, |
3369
|
153 "-*- texinfo -*-\n\ |
|
154 @deftypefn {Loadable Function} {} find (@var{x})\n\ |
|
155 Return a vector of indices of nonzero elements of a matrix. To obtain a\n\ |
|
156 single index for each matrix element, Octave pretends that the columns\n\ |
|
157 of a matrix form one long vector (like Fortran arrays are stored). For\n\ |
|
158 example,\n\ |
|
159 \n\ |
|
160 @example\n\ |
|
161 @group\n\ |
|
162 find (eye (2))\n\ |
|
163 @result{} [ 1; 4 ]\n\ |
|
164 @end group\n\ |
|
165 @end example\n\ |
|
166 \n\ |
|
167 If two outputs are requested, @code{find} returns the row and column\n\ |
|
168 indices of nonzero elements of a matrix. For example,\n\ |
|
169 \n\ |
|
170 @example\n\ |
|
171 @group\n\ |
|
172 [i, j] = find (2 * eye (2))\n\ |
|
173 @result{} i = [ 1; 2 ]\n\ |
|
174 @result{} j = [ 1; 2 ]\n\ |
|
175 @end group\n\ |
|
176 @end example\n\ |
|
177 \n\ |
|
178 If three outputs are requested, @code{find} also returns a vector\n\ |
|
179 containing the nonzero values. For example,\n\ |
|
180 \n\ |
|
181 @example\n\ |
|
182 @group\n\ |
|
183 [i, j, v] = find (3 * eye (2))\n\ |
|
184 @result{} i = [ 1; 2 ]\n\ |
|
185 @result{} j = [ 1; 2 ]\n\ |
|
186 @result{} v = [ 3; 3 ]\n\ |
|
187 @end group\n\ |
|
188 @end example\n\ |
|
189 @end deftypefn") |
2928
|
190 { |
|
191 octave_value_list retval; |
|
192 |
|
193 int nargin = args.length (); |
|
194 |
|
195 if (nargin != 1 || nargout > 3) |
|
196 { |
5823
|
197 print_usage (); |
2928
|
198 return retval; |
|
199 } |
|
200 |
|
201 octave_value arg = args(0); |
|
202 |
|
203 if (arg.is_real_type ()) |
|
204 { |
4678
|
205 NDArray nda = arg.array_value (); |
2928
|
206 |
|
207 if (! error_state) |
4678
|
208 retval = find_nonzero_elem_idx (nda, nargout); |
2928
|
209 } |
|
210 else if (arg.is_complex_type ()) |
|
211 { |
4678
|
212 ComplexNDArray cnda = arg.complex_array_value (); |
2928
|
213 |
|
214 if (! error_state) |
4678
|
215 retval = find_nonzero_elem_idx (cnda, nargout); |
2928
|
216 } |
5107
|
217 else if (arg.is_string ()) |
|
218 { |
|
219 charNDArray cnda = arg.char_array_value (); |
|
220 |
|
221 if (! error_state) |
|
222 retval = find_nonzero_elem_idx (cnda, nargout); |
|
223 } |
2928
|
224 else |
|
225 { |
|
226 gripe_wrong_type_arg ("find", arg); |
|
227 } |
|
228 |
|
229 return retval; |
|
230 } |
|
231 |
|
232 /* |
|
233 ;;; Local Variables: *** |
|
234 ;;; mode: C++ *** |
|
235 ;;; End: *** |
|
236 */ |