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 |
6002
|
35 // Find at most N_TO_FIND nonzero elements in NDA. Search forward if |
|
36 // DIRECTION is 1, backward if it is -1. NARGOUT is the number of |
|
37 // output arguments. If N_TO_FIND is -1, find all nonzero elements. |
4678
|
38 |
|
39 template <typename T> |
|
40 octave_value_list |
6002
|
41 find_nonzero_elem_idx (const T& nda, int nargout, octave_idx_type n_to_find, |
|
42 int direction) |
4678
|
43 { |
6002
|
44 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); |
4678
|
45 |
5275
|
46 octave_idx_type count = 0; |
4678
|
47 |
5275
|
48 octave_idx_type nel = nda.nelem (); |
4678
|
49 |
6002
|
50 // Set the starting element to the correct value based on the |
|
51 // direction to search. |
|
52 octave_idx_type k = 0; |
|
53 if (direction == -1) |
|
54 k = nel - 1; |
|
55 |
|
56 // Search in the default range. |
|
57 octave_idx_type start_el = -1; |
|
58 octave_idx_type end_el = -1; |
|
59 |
|
60 // Search for the number of elements to return. |
|
61 while (k < nel && k > -1 && n_to_find != count) |
4678
|
62 { |
|
63 OCTAVE_QUIT; |
|
64 |
6002
|
65 if (nda(k) != 0.0) |
|
66 { |
|
67 end_el = k; |
|
68 if (start_el == -1) |
|
69 start_el = k; |
|
70 count++; |
|
71 } |
|
72 k = k + direction; |
4678
|
73 } |
|
74 |
6002
|
75 // Reverse the range if we're looking backward. |
|
76 if (direction == -1) |
|
77 { |
|
78 octave_idx_type tmp_el = start_el; |
|
79 start_el = end_el; |
|
80 end_el = tmp_el; |
|
81 } |
|
82 // Fix an off by one error. |
|
83 end_el++; |
|
84 |
4826
|
85 // If the original argument was a row vector, force a row vector of |
5130
|
86 // the overall indices to be returned. But see below for scalar |
|
87 // case... |
4678
|
88 |
5275
|
89 octave_idx_type result_nr = count; |
|
90 octave_idx_type result_nc = 1; |
4678
|
91 |
5130
|
92 bool scalar_arg = false; |
|
93 |
4826
|
94 if (nda.ndims () == 2 && nda.rows () == 1) |
|
95 { |
|
96 result_nr = 1; |
|
97 result_nc = count; |
5130
|
98 |
|
99 scalar_arg = (nda.columns () == 1); |
4826
|
100 } |
3418
|
101 |
4826
|
102 Matrix idx (result_nr, result_nc); |
4678
|
103 |
4826
|
104 Matrix i_idx (result_nr, result_nc); |
|
105 Matrix j_idx (result_nr, result_nc); |
4678
|
106 |
4826
|
107 T val (dim_vector (result_nr, result_nc)); |
|
108 |
|
109 if (count > 0) |
4678
|
110 { |
4826
|
111 count = 0; |
|
112 |
5275
|
113 octave_idx_type nr = nda.rows (); |
4678
|
114 |
5275
|
115 octave_idx_type i = 0; |
4826
|
116 |
6002
|
117 // Search for elements to return. Only search the region where |
|
118 // there are elements to be found using the count that we want |
|
119 // to find. |
6005
|
120 |
|
121 // For compatibility, all N-d arrays are handled as if they are |
|
122 // 2-d, with the number of columns equal to "prod (dims (2:end))". |
|
123 |
6002
|
124 for (k = start_el; k < end_el; k++) |
4678
|
125 { |
4826
|
126 OCTAVE_QUIT; |
4678
|
127 |
4826
|
128 if (nda(k) != 0.0) |
|
129 { |
|
130 idx(count) = k + 1; |
4678
|
131 |
6005
|
132 octave_idx_type xr = k % nr; |
|
133 i_idx(count) = xr + 1; |
|
134 j_idx(count) = (k - xr) / nr + 1; |
4678
|
135 |
4826
|
136 val(count) = nda(k); |
|
137 |
|
138 count++; |
|
139 } |
4678
|
140 |
4826
|
141 i++; |
4678
|
142 } |
|
143 } |
5130
|
144 else if (scalar_arg) |
5131
|
145 { |
|
146 idx.resize (0, 0); |
|
147 |
|
148 i_idx.resize (0, 0); |
|
149 j_idx.resize (0, 0); |
|
150 |
|
151 val.resize (dim_vector (0, 0)); |
|
152 } |
2928
|
153 |
|
154 switch (nargout) |
|
155 { |
6254
|
156 default: |
2928
|
157 case 3: |
|
158 retval(2) = val; |
|
159 // Fall through! |
|
160 |
|
161 case 2: |
3418
|
162 retval(1) = j_idx; |
|
163 retval(0) = i_idx; |
2928
|
164 break; |
|
165 |
6254
|
166 case 1: |
|
167 case 0: |
|
168 retval(0) = idx; |
2928
|
169 break; |
|
170 } |
|
171 |
|
172 return retval; |
|
173 } |
|
174 |
6002
|
175 template octave_value_list find_nonzero_elem_idx (const NDArray&, int, |
|
176 octave_idx_type, int); |
2928
|
177 |
6002
|
178 template octave_value_list find_nonzero_elem_idx (const ComplexNDArray&, int, |
|
179 octave_idx_type, int); |
2928
|
180 |
|
181 DEFUN_DLD (find, args, nargout, |
3369
|
182 "-*- texinfo -*-\n\ |
|
183 @deftypefn {Loadable Function} {} find (@var{x})\n\ |
6002
|
184 @deftypefnx {Loadable Function} {} find (@var{x}, @var{n})\n\ |
|
185 @deftypefnx {Loadable Function} {} find (@var{x}, @var{n}, @var{direction})\n\ |
3369
|
186 Return a vector of indices of nonzero elements of a matrix. To obtain a\n\ |
|
187 single index for each matrix element, Octave pretends that the columns\n\ |
|
188 of a matrix form one long vector (like Fortran arrays are stored). For\n\ |
|
189 example,\n\ |
|
190 \n\ |
|
191 @example\n\ |
|
192 @group\n\ |
|
193 find (eye (2))\n\ |
|
194 @result{} [ 1; 4 ]\n\ |
|
195 @end group\n\ |
|
196 @end example\n\ |
|
197 \n\ |
|
198 If two outputs are requested, @code{find} returns the row and column\n\ |
|
199 indices of nonzero elements of a matrix. For example,\n\ |
|
200 \n\ |
|
201 @example\n\ |
|
202 @group\n\ |
|
203 [i, j] = find (2 * eye (2))\n\ |
|
204 @result{} i = [ 1; 2 ]\n\ |
|
205 @result{} j = [ 1; 2 ]\n\ |
|
206 @end group\n\ |
|
207 @end example\n\ |
|
208 \n\ |
|
209 If three outputs are requested, @code{find} also returns a vector\n\ |
|
210 containing the nonzero values. For example,\n\ |
|
211 \n\ |
|
212 @example\n\ |
|
213 @group\n\ |
|
214 [i, j, v] = find (3 * eye (2))\n\ |
|
215 @result{} i = [ 1; 2 ]\n\ |
|
216 @result{} j = [ 1; 2 ]\n\ |
|
217 @result{} v = [ 3; 3 ]\n\ |
|
218 @end group\n\ |
|
219 @end example\n\ |
6002
|
220 \n\ |
|
221 If two inputs are given, @var{n} indicates the number of elements to\n\ |
|
222 find from the beginning of the matrix or vector.\n\ |
|
223 \n\ |
|
224 If three inputs are given, @var{direction} should be one of \"first\" or\n\ |
|
225 \"last\" indicating that it should start counting found elements from the\n\ |
|
226 first or last element.\n\ |
3369
|
227 @end deftypefn") |
2928
|
228 { |
|
229 octave_value_list retval; |
|
230 |
|
231 int nargin = args.length (); |
|
232 |
6002
|
233 if (nargin > 3 || nargin < 1) |
2928
|
234 { |
5823
|
235 print_usage (); |
2928
|
236 return retval; |
|
237 } |
|
238 |
6002
|
239 // Setup the default options. |
|
240 octave_idx_type n_to_find = -1; |
|
241 if (nargin > 1) |
|
242 { |
|
243 n_to_find = args(1).int_value (); |
|
244 if (error_state) |
|
245 { |
|
246 error ("find: expecting second argument to be an integer"); |
|
247 return retval; |
|
248 } |
|
249 } |
|
250 |
|
251 // Direction to do the searching (1 == forward, -1 == reverse). |
|
252 int direction = 1; |
|
253 if (nargin > 2) |
|
254 { |
|
255 direction = 0; |
|
256 |
|
257 std::string s_arg = args(2).string_value (); |
|
258 |
|
259 if (! error_state) |
|
260 { |
|
261 if (s_arg == "first") |
|
262 direction = 1; |
|
263 else if (s_arg == "last") |
|
264 direction = -1; |
|
265 } |
|
266 |
|
267 if (direction == 0) |
|
268 { |
|
269 error ("find: expecting third argument to be \"first\" or \"last\""); |
|
270 return retval; |
|
271 } |
|
272 } |
|
273 |
2928
|
274 octave_value arg = args(0); |
|
275 |
|
276 if (arg.is_real_type ()) |
|
277 { |
4678
|
278 NDArray nda = arg.array_value (); |
2928
|
279 |
|
280 if (! error_state) |
6002
|
281 retval = find_nonzero_elem_idx (nda, nargout, n_to_find, direction); |
2928
|
282 } |
|
283 else if (arg.is_complex_type ()) |
|
284 { |
4678
|
285 ComplexNDArray cnda = arg.complex_array_value (); |
2928
|
286 |
|
287 if (! error_state) |
6002
|
288 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); |
2928
|
289 } |
5107
|
290 else if (arg.is_string ()) |
|
291 { |
|
292 charNDArray cnda = arg.char_array_value (); |
|
293 |
|
294 if (! error_state) |
6002
|
295 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); |
5107
|
296 } |
2928
|
297 else |
|
298 { |
|
299 gripe_wrong_type_arg ("find", arg); |
|
300 } |
|
301 |
|
302 return retval; |
|
303 } |
|
304 |
|
305 /* |
|
306 ;;; Local Variables: *** |
|
307 ;;; mode: C++ *** |
|
308 ;;; End: *** |
|
309 */ |