5164
|
1 ## Copyright (C) 2004 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software and is in the public domain |
|
4 |
|
5 ## -*- texinfo -*- |
6547
|
6 ## @deftypefn {Function File} {[@var{count}, @var{mean}, @var{var}] =} spstats (@var{s}) |
|
7 ## @deftypefnx {Function File} {[@var{count}, @var{mean}, @var{var}] =} spstats (@var{s}, @var{j}) |
5164
|
8 ## Return the stats for the non-zero elements of the sparse matrix @var{s}. |
|
9 ## @var{count} is the number of non-zeros in each column, @var{mean} |
|
10 ## is the mean of the non-zeros in each column, and @var{var} is the |
|
11 ## variance of the non-zeros in each column. |
|
12 ## |
5584
|
13 ## Called with two input arguments, if @var{s} is the data and @var{j} |
5164
|
14 ## is the bin number for the data, compute the stats for each bin. In |
|
15 ## this case, bins can contain data values of zero, whereas with |
|
16 ## @code{spstats (@var{s})} the zeros may disappear. |
|
17 ## @end deftypefn |
|
18 |
6498
|
19 function [count, mean, var] = spstats (S, j) |
|
20 |
|
21 if (nargin < 1 || nargin > 2) |
6046
|
22 print_usage (); |
5164
|
23 endif |
|
24 |
6498
|
25 if (nargin == 1) |
|
26 [i, j, v] = find (S); |
5164
|
27 else |
|
28 v = S; |
|
29 i = 1:length (v); |
|
30 S = sparse (i, j, v); |
|
31 endif |
5584
|
32 [n, m] = size (S); |
5164
|
33 |
|
34 count = spsum (sparse (i, j, 1, n, m)); |
|
35 if (nargout > 1) |
6498
|
36 mean = spsum (S) ./ count; |
5164
|
37 end |
|
38 if (nargout > 2) |
5775
|
39 ## FIXME Variance with count = 0 or 1? |
5164
|
40 diff = S - sparse (i, j, mean(j), n, m); |
|
41 var = spsum (diff .* diff) ./ (count - 1); |
|
42 end |
|
43 endfunction |
|
44 |
|
45 %!test |
|
46 %! [n,m,v] = spstats([1 2 1 2 3 4],[2 2 1 1 1 1]); |
|
47 %! assert(n,[4,2]); |
|
48 %! assert(m,[10/4,3/2],10*eps); |
|
49 %! assert(v,[5/3,1/2],10*eps); |