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; |
|
116 octave_idx_type j = 0; |
4826
|
117 |
6002
|
118 // Search for elements to return. Only search the region where |
|
119 // there are elements to be found using the count that we want |
|
120 // to find. |
|
121 for (k = start_el; k < end_el; k++) |
4678
|
122 { |
4826
|
123 OCTAVE_QUIT; |
4678
|
124 |
4826
|
125 if (nda(k) != 0.0) |
|
126 { |
|
127 idx(count) = k + 1; |
4678
|
128 |
4826
|
129 i_idx(count) = i + 1; |
|
130 j_idx(count) = j + 1; |
4678
|
131 |
4826
|
132 val(count) = nda(k); |
|
133 |
|
134 count++; |
|
135 } |
4678
|
136 |
4826
|
137 i++; |
4678
|
138 |
4826
|
139 if (i == nr) |
|
140 { |
|
141 i = 0; |
|
142 |
|
143 j++; |
|
144 } |
4678
|
145 } |
|
146 } |
5130
|
147 else if (scalar_arg) |
5131
|
148 { |
|
149 idx.resize (0, 0); |
|
150 |
|
151 i_idx.resize (0, 0); |
|
152 j_idx.resize (0, 0); |
|
153 |
|
154 val.resize (dim_vector (0, 0)); |
|
155 } |
2928
|
156 |
|
157 switch (nargout) |
|
158 { |
|
159 case 0: |
|
160 case 1: |
4826
|
161 retval(0) = idx; |
2928
|
162 break; |
|
163 |
|
164 case 3: |
|
165 retval(2) = val; |
|
166 // Fall through! |
|
167 |
|
168 case 2: |
3418
|
169 retval(1) = j_idx; |
|
170 retval(0) = i_idx; |
2928
|
171 break; |
|
172 |
|
173 default: |
|
174 panic_impossible (); |
|
175 break; |
|
176 } |
|
177 |
|
178 return retval; |
|
179 } |
|
180 |
6002
|
181 template octave_value_list find_nonzero_elem_idx (const NDArray&, int, |
|
182 octave_idx_type, int); |
2928
|
183 |
6002
|
184 template octave_value_list find_nonzero_elem_idx (const ComplexNDArray&, int, |
|
185 octave_idx_type, int); |
2928
|
186 |
|
187 DEFUN_DLD (find, args, nargout, |
3369
|
188 "-*- texinfo -*-\n\ |
|
189 @deftypefn {Loadable Function} {} find (@var{x})\n\ |
6002
|
190 @deftypefnx {Loadable Function} {} find (@var{x}, @var{n})\n\ |
|
191 @deftypefnx {Loadable Function} {} find (@var{x}, @var{n}, @var{direction})\n\ |
3369
|
192 Return a vector of indices of nonzero elements of a matrix. To obtain a\n\ |
|
193 single index for each matrix element, Octave pretends that the columns\n\ |
|
194 of a matrix form one long vector (like Fortran arrays are stored). For\n\ |
|
195 example,\n\ |
|
196 \n\ |
|
197 @example\n\ |
|
198 @group\n\ |
|
199 find (eye (2))\n\ |
|
200 @result{} [ 1; 4 ]\n\ |
|
201 @end group\n\ |
|
202 @end example\n\ |
|
203 \n\ |
|
204 If two outputs are requested, @code{find} returns the row and column\n\ |
|
205 indices of nonzero elements of a matrix. For example,\n\ |
|
206 \n\ |
|
207 @example\n\ |
|
208 @group\n\ |
|
209 [i, j] = find (2 * eye (2))\n\ |
|
210 @result{} i = [ 1; 2 ]\n\ |
|
211 @result{} j = [ 1; 2 ]\n\ |
|
212 @end group\n\ |
|
213 @end example\n\ |
|
214 \n\ |
|
215 If three outputs are requested, @code{find} also returns a vector\n\ |
|
216 containing the nonzero values. For example,\n\ |
|
217 \n\ |
|
218 @example\n\ |
|
219 @group\n\ |
|
220 [i, j, v] = find (3 * eye (2))\n\ |
|
221 @result{} i = [ 1; 2 ]\n\ |
|
222 @result{} j = [ 1; 2 ]\n\ |
|
223 @result{} v = [ 3; 3 ]\n\ |
|
224 @end group\n\ |
|
225 @end example\n\ |
6002
|
226 \n\ |
|
227 If two inputs are given, @var{n} indicates the number of elements to\n\ |
|
228 find from the beginning of the matrix or vector.\n\ |
|
229 \n\ |
|
230 If three inputs are given, @var{direction} should be one of \"first\" or\n\ |
|
231 \"last\" indicating that it should start counting found elements from the\n\ |
|
232 first or last element.\n\ |
3369
|
233 @end deftypefn") |
2928
|
234 { |
|
235 octave_value_list retval; |
|
236 |
|
237 int nargin = args.length (); |
|
238 |
6002
|
239 if (nargin > 3 || nargin < 1) |
2928
|
240 { |
5823
|
241 print_usage (); |
2928
|
242 return retval; |
|
243 } |
|
244 |
6002
|
245 // Setup the default options. |
|
246 octave_idx_type n_to_find = -1; |
|
247 if (nargin > 1) |
|
248 { |
|
249 n_to_find = args(1).int_value (); |
|
250 if (error_state) |
|
251 { |
|
252 error ("find: expecting second argument to be an integer"); |
|
253 return retval; |
|
254 } |
|
255 } |
|
256 |
|
257 // Direction to do the searching (1 == forward, -1 == reverse). |
|
258 int direction = 1; |
|
259 if (nargin > 2) |
|
260 { |
|
261 direction = 0; |
|
262 |
|
263 std::string s_arg = args(2).string_value (); |
|
264 |
|
265 if (! error_state) |
|
266 { |
|
267 if (s_arg == "first") |
|
268 direction = 1; |
|
269 else if (s_arg == "last") |
|
270 direction = -1; |
|
271 } |
|
272 |
|
273 if (direction == 0) |
|
274 { |
|
275 error ("find: expecting third argument to be \"first\" or \"last\""); |
|
276 return retval; |
|
277 } |
|
278 } |
|
279 |
2928
|
280 octave_value arg = args(0); |
|
281 |
|
282 if (arg.is_real_type ()) |
|
283 { |
4678
|
284 NDArray nda = arg.array_value (); |
2928
|
285 |
|
286 if (! error_state) |
6002
|
287 retval = find_nonzero_elem_idx (nda, nargout, n_to_find, direction); |
2928
|
288 } |
|
289 else if (arg.is_complex_type ()) |
|
290 { |
4678
|
291 ComplexNDArray cnda = arg.complex_array_value (); |
2928
|
292 |
|
293 if (! error_state) |
6002
|
294 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); |
2928
|
295 } |
5107
|
296 else if (arg.is_string ()) |
|
297 { |
|
298 charNDArray cnda = arg.char_array_value (); |
|
299 |
|
300 if (! error_state) |
6002
|
301 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); |
5107
|
302 } |
2928
|
303 else |
|
304 { |
|
305 gripe_wrong_type_arg ("find", arg); |
|
306 } |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
|
311 /* |
|
312 ;;; Local Variables: *** |
|
313 ;;; mode: C++ *** |
|
314 ;;; End: *** |
|
315 */ |