]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/squareform.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / squareform.m
1 ## Copyright (C) 2006, 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{y} =} squareform (@var{x})
18 ## @deftypefnx {Function File} {@var{y} =} squareform (@var{x}, @
19 ## "tovector")
20 ## @deftypefnx {Function File} {@var{y} =} squareform (@var{x}, @
21 ## "tomatrix")
22 ## Convert a vector from the pdist function into a square matrix or from
23 ## a square matrix back to the vector form.
24 ##
25 ## The second argument is used to specify the output type in case there
26 ## is a single element.
27 ## @seealso{pdist}
28 ## @end deftypefn
29
30 ## Author: Bill Denney <bill@denney.ws>
31
32 function y = squareform (x, method)
33
34   if nargin < 1
35     print_usage ();
36   elseif nargin < 2
37     if isscalar (x) || isvector (x)
38       method = "tomatrix";
39     elseif issquare (x)
40       method = "tovector";
41     else
42       error ("squareform: cannot deal with a nonsquare, nonvector \
43        input");
44     endif
45   endif
46   method = lower (method);
47
48   if ! strcmp ({"tovector" "tomatrix"}, method)
49     error ("squareform: method must be either \"tovector\" or \
50        \"tomatrix\"");
51   endif
52
53   if strcmp ("tovector", method)
54     if ! issquare (x)
55       error ("squareform: x is not a square matrix");
56     endif
57
58     sx = size (x, 1);
59     y = zeros ((sx-1)*sx/2, 1);
60     idx = 1;
61     for i = 2:sx
62       newidx = idx + sx - i;
63       y(idx:newidx) = x(i:sx,i-1);
64       idx = newidx + 1;
65     endfor
66   else
67     ## we're converting to a matrix
68
69     ## make sure that x is a column
70     x = x(:);
71
72     ## the dimensions of y are the solution to the quadratic formula
73     ## for:
74     ## length(x) = (sy-1)*(sy/2)
75     sy = (1 + sqrt (1+ 8*length (x)))/2;
76     y = zeros (sy);
77     for i = 1:sy-1
78       step = sy - i;
79       y((sy-step+1):sy,i) = x(1:step);
80       x(1:step) = [];
81     endfor
82     y = y + y';
83   endif
84
85 endfunction
86
87 ## make sure that it can go both directions automatically
88 %!assert(squareform(1:6), [0 1 2 3;1 0 4 5;2 4 0 6;3 5 6 0])
89 %!assert(squareform([0 1 2 3;1 0 4 5;2 4 0 6;3 5 6 0]), [1:6]')
90
91 ## make sure that the command arguments force the correct behavior
92 %!assert(squareform(1), [0 1;1 0])
93 %!assert(squareform(1, "tomatrix"), [0 1;1 0])
94 %!assert(squareform(1, "tovector"), zeros(0,1))