--- a/inst/unvech.m
+++ b/inst/unvech.m
@@ -15,7 +15,7 @@
 ##
 ## You should have received a copy of the GNU General Public License
 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
-
+ 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {@var{m} =} unvech (@var{v}, @var{scale})
 ## Performs the reverse of @code{vech} on the vector @var{v}.
@@ -27,31 +27,44 @@
 ## that 1 and -1 can be used for symmetric and antisymmetric matrix respectively.
 ## @var{scale} must be a scalar and defaults to 1.
 ##
-## @seealso{vech, ind2sub, sub2ind_tril}
+## @seealso{vech, ind2sub}
 ## @end deftypefn
-
+ 
 function M = unvech (v, scale = 1)
-
+ 
   if ( nargin < 1 || nargin > 2 )
     print_usage;
   elseif ( !ismatrix (v) && any (size (v) != 1) )
-    error ("V must be a row orcolumn matrix")
+    error ("V must be a row or column matrix")
   elseif ( !isnumeric (scale) || !isscalar (scale) )
     error ("SCALE must be a scalar")
   endif
-
+ 
   N      = length (v);
   dim    = (sqrt ( 1 + 8*N ) - 1)/2;
   [r, c] = ind2sub_tril (dim, 1:N);
   M      = accumarray ([r; c].', v);
   M     += scale * tril (M, -1).';
-
+ 
 endfunction
 
+function [r c] = ind2sub_tril(N,idx)
+
+  %% Horrible lengthly check
+  if nargin < 2 ||( !isnumeric (N) && all(size(N)==1) )|| ...
+    !( ismatrix (idx) && any (size (idx)==1) ) 
+    print_usage;
+  endif
+  
+  endofrow = 0.5*(1:N) .* (2*N:-1:N + 1);
+  c = lookup(endofrow, idx-1)+1;
+
+  r = N - endofrow(c) + idx ;
+
+end
+
 %!assert(unvech([1;0;0;1;0;1]), full(eye(3,3)) );
-
-#%!error <does not generate a square matrix> unvech([1;0;0;1;0;1;1]);
-
+ 
 %!test %symmetric
 %! dim = 10;
 %! A = tril( floor ( 5*(2*rand(dim)-1) ) );
@@ -59,7 +72,7 @@
 %! M = vech(A);
 %! M = unvech(M, 1);
 %! assert (A, M);
-
+ 
 %!test %antisymmetric
 %! dim = 10;
 %! A = tril( floor ( 5*(2*rand(dim)-1) ) );
@@ -67,3 +80,4 @@
 %! M = vech(A);
 %! M = unvech(M, -1);
 %! assert (A, M);
+