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