2540
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
2540
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
2540
|
8 ## This program 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 |
3426
|
11 ## General Public License for more details. |
|
12 ## |
2540
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3369
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} vech (@var{x}) |
|
19 ## Return the vector obtained by eliminating all supradiagonal elements of |
|
20 ## the square matrix @var{x} and stacking the result one column above the |
|
21 ## other. |
|
22 ## @end deftypefn |
|
23 |
2540
|
24 ## See Magnus and Neudecker (1988), Matrix differential calculus with |
|
25 ## applications in statistics and econometrics. |
|
26 |
|
27 ## Author KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
28 ## Created: 8 May 1995 |
|
29 ## Adapted-By: jwe |
|
30 |
|
31 function v = vech (x) |
3426
|
32 |
2540
|
33 if (nargin != 1) |
|
34 usage ("vech (x)"); |
|
35 endif |
3426
|
36 |
2540
|
37 if (! is_square (x)) |
|
38 error ("vech: x must be square"); |
|
39 endif |
3426
|
40 |
2540
|
41 ## This should be quicker than having an inner `for' loop as well. |
|
42 ## Ideally, vech should be written in C++. |
|
43 n = rows (x); |
2745
|
44 v = zeros ((n+1)*n/2, 1); |
2540
|
45 count = 0; |
|
46 for j = 1 : n |
3426
|
47 i = j : n; |
2540
|
48 v (count + i) = x (i, j); |
|
49 count = count + n - j; |
|
50 endfor |
|
51 |
|
52 endfunction |