On Wed, 26 Apr 2017, Antonio Di Paolo wrote:
Dear GRETL users,
I would like to create a diagonal matrix M created from a vector V, that
is I have a vector of dimension “n” and I would like to obtain an nXn
matrix with the elements of the vector V in the main diagonal of the
matrix X (which contains zeros outside the main diagonal).
This can be done in Stata with the “diag” command or in Eviews with the
“@makediagonal” command, but I’m still unable to find the same command
in GRETL.
Any suggestion? Thanks in advance.
An easy way to do this is to use the "diag" slicing operator, as in
<hansl>
matrix B = zeros(3,3)
B[diag] = seq(1,3)
</hansl>
Note, however, that in actual practice diagonal matrices are rarely
indispensable. In matrix algebra, you normally use diagonal matrices as
tools for rescaling rows (columns) of a matrix via
pre-(post-)multiplication. This is unnecessary in hansl: it's much cleaner
and faster to use the .* operator. For example, try this:
<hansl>
matrix A = mshape(seq(1,9),3,3)
matrix c = {1; 0; -1}
matrix B = c .* A
print A B c
</hansl>
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------