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 |
|
55 // the overall indices to be returned. |
4678
|
56 |
4826
|
57 int result_nr = count; |
|
58 int result_nc = 1; |
4678
|
59 |
4826
|
60 if (nda.ndims () == 2 && nda.rows () == 1) |
|
61 { |
|
62 result_nr = 1; |
|
63 result_nc = count; |
|
64 } |
3418
|
65 |
4826
|
66 Matrix idx (result_nr, result_nc); |
4678
|
67 |
4826
|
68 Matrix i_idx (result_nr, result_nc); |
|
69 Matrix j_idx (result_nr, result_nc); |
4678
|
70 |
4826
|
71 T val (dim_vector (result_nr, result_nc)); |
|
72 |
|
73 if (count > 0) |
4678
|
74 { |
4826
|
75 count = 0; |
|
76 |
|
77 int nr = nda.rows (); |
4678
|
78 |
4826
|
79 int i = 0; |
|
80 int j = 0; |
|
81 |
|
82 for (int k = 0; k < nel; k++) |
4678
|
83 { |
4826
|
84 OCTAVE_QUIT; |
4678
|
85 |
4826
|
86 if (nda(k) != 0.0) |
|
87 { |
|
88 idx(count) = k + 1; |
4678
|
89 |
4826
|
90 i_idx(count) = i + 1; |
|
91 j_idx(count) = j + 1; |
4678
|
92 |
4826
|
93 val(count) = nda(k); |
|
94 |
|
95 count++; |
|
96 } |
4678
|
97 |
4826
|
98 i++; |
4678
|
99 |
4826
|
100 if (i == nr) |
|
101 { |
|
102 i = 0; |
|
103 |
|
104 j++; |
|
105 } |
4678
|
106 } |
|
107 } |
2928
|
108 |
|
109 switch (nargout) |
|
110 { |
|
111 case 0: |
|
112 case 1: |
4826
|
113 retval(0) = idx; |
2928
|
114 break; |
|
115 |
|
116 case 3: |
|
117 retval(2) = val; |
|
118 // Fall through! |
|
119 |
|
120 case 2: |
3418
|
121 retval(1) = j_idx; |
|
122 retval(0) = i_idx; |
2928
|
123 break; |
|
124 |
|
125 default: |
|
126 panic_impossible (); |
|
127 break; |
|
128 } |
|
129 |
|
130 return retval; |
|
131 } |
|
132 |
4678
|
133 template octave_value_list find_nonzero_elem_idx (const NDArray&, int); |
2928
|
134 |
4678
|
135 template octave_value_list find_nonzero_elem_idx (const ComplexNDArray&, int); |
2928
|
136 |
|
137 DEFUN_DLD (find, args, nargout, |
3369
|
138 "-*- texinfo -*-\n\ |
|
139 @deftypefn {Loadable Function} {} find (@var{x})\n\ |
|
140 Return a vector of indices of nonzero elements of a matrix. To obtain a\n\ |
|
141 single index for each matrix element, Octave pretends that the columns\n\ |
|
142 of a matrix form one long vector (like Fortran arrays are stored). For\n\ |
|
143 example,\n\ |
|
144 \n\ |
|
145 @example\n\ |
|
146 @group\n\ |
|
147 find (eye (2))\n\ |
|
148 @result{} [ 1; 4 ]\n\ |
|
149 @end group\n\ |
|
150 @end example\n\ |
|
151 \n\ |
|
152 If two outputs are requested, @code{find} returns the row and column\n\ |
|
153 indices of nonzero elements of a matrix. For example,\n\ |
|
154 \n\ |
|
155 @example\n\ |
|
156 @group\n\ |
|
157 [i, j] = find (2 * eye (2))\n\ |
|
158 @result{} i = [ 1; 2 ]\n\ |
|
159 @result{} j = [ 1; 2 ]\n\ |
|
160 @end group\n\ |
|
161 @end example\n\ |
|
162 \n\ |
|
163 If three outputs are requested, @code{find} also returns a vector\n\ |
|
164 containing the nonzero values. For example,\n\ |
|
165 \n\ |
|
166 @example\n\ |
|
167 @group\n\ |
|
168 [i, j, v] = find (3 * eye (2))\n\ |
|
169 @result{} i = [ 1; 2 ]\n\ |
|
170 @result{} j = [ 1; 2 ]\n\ |
|
171 @result{} v = [ 3; 3 ]\n\ |
|
172 @end group\n\ |
|
173 @end example\n\ |
|
174 @end deftypefn") |
2928
|
175 { |
|
176 octave_value_list retval; |
|
177 |
|
178 int nargin = args.length (); |
|
179 |
|
180 if (nargin != 1 || nargout > 3) |
|
181 { |
|
182 print_usage ("find"); |
|
183 return retval; |
|
184 } |
|
185 |
|
186 octave_value arg = args(0); |
|
187 |
|
188 if (arg.is_real_type ()) |
|
189 { |
4678
|
190 NDArray nda = arg.array_value (); |
2928
|
191 |
|
192 if (! error_state) |
4678
|
193 retval = find_nonzero_elem_idx (nda, nargout); |
2928
|
194 } |
|
195 else if (arg.is_complex_type ()) |
|
196 { |
4678
|
197 ComplexNDArray cnda = arg.complex_array_value (); |
2928
|
198 |
|
199 if (! error_state) |
4678
|
200 retval = find_nonzero_elem_idx (cnda, nargout); |
2928
|
201 } |
|
202 else |
|
203 { |
|
204 gripe_wrong_type_arg ("find", arg); |
|
205 } |
|
206 |
|
207 return retval; |
|
208 } |
|
209 |
|
210 /* |
|
211 ;;; Local Variables: *** |
|
212 ;;; mode: C++ *** |
|
213 ;;; End: *** |
|
214 */ |