PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/octave-forge/main/statistics/inst/squareform.m

#
MATLAB | 95 lines | 80 code | 10 blank | 5 comment | 10 complexity | d5dd056eb426e3daa834a651732dd225 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. ## Copyright (C) 2006, 2008 Bill Denney <bill@denney.ws>
  2. ##
  3. ## This 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
  5. ## Software Foundation; either version 2, or (at your option) any later
  6. ## version.
  7. ##
  8. ## This software is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ## General Public License for more details.
  12. ##
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this software; see the file COPYING. If not, see
  15. ## <http://www.gnu.org/licenses/>.
  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. ## Author: Bill Denney <bill@denney.ws>
  30. function y = squareform (x, method)
  31. if nargin < 1
  32. print_usage ();
  33. elseif nargin < 2
  34. if isscalar (x) || isvector (x)
  35. method = "tomatrix";
  36. elseif issquare (x)
  37. method = "tovector";
  38. else
  39. error ("squareform: cannot deal with a nonsquare, nonvector \
  40. input");
  41. endif
  42. endif
  43. method = lower (method);
  44. if ! strcmp ({"tovector" "tomatrix"}, method)
  45. error ("squareform: method must be either \"tovector\" or \
  46. \"tomatrix\"");
  47. endif
  48. if strcmp ("tovector", method)
  49. if ! issquare (x)
  50. error ("squareform: x is not a square matrix");
  51. endif
  52. sx = size (x, 1);
  53. y = zeros ((sx-1)*sx/2, 1);
  54. idx = 1;
  55. for i = 2:sx
  56. newidx = idx + sx - i;
  57. y(idx:newidx) = x(i:sx,i-1);
  58. idx = newidx + 1;
  59. endfor
  60. else
  61. ## we're converting to a matrix
  62. ## make sure that x is a column
  63. x = x(:);
  64. ## the dimensions of y are the solution to the quadratic formula
  65. ## for:
  66. ## length(x) = (sy-1)*(sy/2)
  67. sy = (1 + sqrt (1+ 8*length (x)))/2;
  68. y = zeros (sy);
  69. for i = 1:sy-1
  70. step = sy - i;
  71. y((sy-step+1):sy,i) = x(1:step);
  72. x(1:step) = [];
  73. endfor
  74. y = y + y';
  75. endif
  76. endfunction
  77. ## make sure that it can go both directions automatically
  78. %!assert(squareform(1:6), [0 1 2 3;1 0 4 5;2 4 0 6;3 5 6 0])
  79. %!assert(squareform([0 1 2 3;1 0 4 5;2 4 0 6;3 5 6 0]), [1:6]')
  80. ## make sure that the command arguments force the correct behavior
  81. %!assert(squareform(1), [0 1;1 0])
  82. %!assert(squareform(1, "tomatrix"), [0 1;1 0])
  83. %!assert(squareform(1, "tovector"), zeros(0,1))