5164
|
1 ## Copyright (C) 2004 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software and is in the public domain |
|
4 |
|
5 ## -*- texinfo -*- |
|
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}) |
|
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 |
|
19 function [count,mean,var] = spstats(S,j) |
|
20 if nargin < 1 || nargin > 2 |
|
21 usage("[count, mean, var] = spstats(S) OR spstats(x,j)"); |
|
22 endif |
|
23 |
|
24 if nargin == 1 |
5584
|
25 [i,j,v] = find (S); |
5164
|
26 else |
|
27 v = S; |
|
28 i = 1:length (v); |
|
29 S = sparse (i, j, v); |
|
30 endif |
5584
|
31 [n, m] = size (S); |
5164
|
32 |
|
33 count = spsum (sparse (i, j, 1, n, m)); |
|
34 if (nargout > 1) |
|
35 mean = spsum(S) ./ count; |
|
36 end |
|
37 if (nargout > 2) |
|
38 ## XXX FIXME XXX Variance with count = 0 or 1? |
|
39 diff = S - sparse (i, j, mean(j), n, m); |
|
40 var = spsum (diff .* diff) ./ (count - 1); |
|
41 end |
|
42 endfunction |
|
43 |
|
44 %!test |
|
45 %! [n,m,v] = spstats([1 2 1 2 3 4],[2 2 1 1 1 1]); |
|
46 %! assert(n,[4,2]); |
|
47 %! assert(m,[10/4,3/2],10*eps); |
|
48 %! assert(v,[5/3,1/2],10*eps); |