From: <hi...@us...> - 2009-02-18 13:28:55
|
Revision: 5557 http://octave.svn.sourceforge.net/octave/?rev=5557&view=rev Author: highegg Date: 2009-02-18 13:28:51 +0000 (Wed, 18 Feb 2009) Log Message: ----------- optimize & adjust style of unvech Modified Paths: -------------- trunk/octave-forge/main/general/inst/unvech.m Modified: trunk/octave-forge/main/general/inst/unvech.m =================================================================== --- trunk/octave-forge/main/general/inst/unvech.m 2009-02-11 21:02:23 UTC (rev 5556) +++ trunk/octave-forge/main/general/inst/unvech.m 2009-02-18 13:28:51 UTC (rev 5557) @@ -1,4 +1,5 @@ ## Copyright (C) 2006 Michael Creel <mic...@ua...> +## Copyright (C) 2009 Jaroslav Hajek ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -19,42 +20,31 @@ ## triangular elements, received as a vector @var{v}. ## @end deftypefn -# Note: this uses a double loop. A C version would be a lot faster for large matrices. -## Adapted-By: Ben Hall <ben...@pw...> - function x = unvech (v) - if (nargin != 1) - usage ("unvech (v)"); - endif + if (nargin != 1) + usage ("unvech (v)"); + endif + + if (! isvector(v)) + usage ("unvech (v)"); + endif + + # find out dimension of symmetric matrix + p = length (v); + n = -(1 - sqrt (1 + 8*p))/2; + + if (mod (n, 1) != 0) + error("unvech: the input vector does not generate a square matrix"); + endif + + x = zeros (n, n); - if (! isvector(v)) - usage ("unvech (v)"); - endif - - # find out dimension of symmetric matrix - p = length(v); - g = -(1 - sqrt(1 + 8*p))/2; - - if (mod(g,1) != 0) - error("unvech: the input vector does not generate a square matrix"); - endif - - x = zeros(g,g); - - # fill in the symmetric matrix, the obvious way -# k = 1; -# for i = 1:g -# for j = i:g -# x(i,j) = v(k); -# if (i != j) x(j,i) = v(k); endif -# k = k + 1; -# endfor -# endfor - - # fill in the symmetric matrix, a more clever way - ii = repmat( 1:g, [g 1] ); - idx= find( ii <= ii' ); - x(idx) = v; - x = x + x' - diag(diag(x)); + # do the reverse of vech + count = 0; + for j = 1 : n + i = j : n; + x(j,i) = x(i,j) = v(count + i); + count += n - j; + endfor endfunction This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |