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