comparison doc/interpreter/sparse.txi @ 6421:cac156381f81

[project @ 2007-03-20 17:39:19 by jwe]
author jwe
date Tue, 20 Mar 2007 17:39:19 +0000
parents 2f64090cdc14
children ff87ad14403f
comparison
equal deleted inserted replaced
6420:e35658098bbc 6421:cac156381f81
204 The recommended way for the user to create a sparse matrix, is to create 204 The recommended way for the user to create a sparse matrix, is to create
205 two vectors containing the row and column index of the data and a third 205 two vectors containing the row and column index of the data and a third
206 vector of the same size containing the data to be stored. For example 206 vector of the same size containing the data to be stored. For example
207 207
208 @example 208 @example
209 function x = foo (r, j) 209 ri = ci = d = [];
210 idx = randperm (r); 210 for j = 1:c
211 x = ([zeros(r-2,1); rand(2,1)])(idx); 211 ri = [ri; randperm(r)(1:n)'];
212 endfunction 212 ci = [ci; j*ones(n,1)];
213 213 d = [d; rand(n,1)];
214 ri = []; 214 endfor
215 ci = []; 215 s = sparse (ri, ci, d, r, c);
216 d = []; 216 @end example
217 217
218 for j=1:c 218 creates an @var{r}-by-@var{c} sparse matrix with a random distribution
219 dtmp = foo (r, j); # Returns vector of length r with (:,j) values 219 of @var{n} (<@var{r}) elements per column. The elements of the vectors
220 idx = find (dtmp != 0.); 220 do not need to be sorted in any particular order as Octave will sort
221 ri = [ri; idx]; 221 them prior to storing the data. However, pre-sorting the data will
222 ci = [ci; j*ones(length(idx),1)]; 222 make the creation of the sparse matrix faster.
223 d = [d; dtmp(idx)];
224 endfor
225 s = sparse (ri, ci, d, r, c);
226 @end example
227
228 creates an @var{r}-by-@var{c} sparse matrix with a random distribution of
229 2 elements per row. The elements of the vectors do not need to be sorted in
230 any particular order as Octave will sort them prior to storing the
231 data. However, pre-sorting the data will make the creation of the sparse
232 matrix faster.
233 223
234 The function @dfn{spconvert} takes a three or four column real matrix. 224 The function @dfn{spconvert} takes a three or four column real matrix.
235 The first two columns represent the row and column index respectively and 225 The first two columns represent the row and column index respectively and
236 the third and four columns, the real and imaginary parts of the sparse 226 the third and four columns, the real and imaginary parts of the sparse
237 matrix. The matrix can contain zero elements and the elements can be 227 matrix. The matrix can contain zero elements and the elements can be