[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: re: matrix manipulation

From   Kit Baum <>
Subject   st: re: matrix manipulation
Date   Fri, 12 Dec 2008 10:54:14 -0500

< >
As Austin notes, this can be done as a function. He points out the way to avoid a loop to zap the diagonal. I would rather do it as a Mata function than an inline call. Both work, but the inline call IMHO leads to write-only code. The function approach is attractive in that you can readily clone the function and tweak it when you have something a bit different (and perhaps more complicated) to achieve.

In any case, whether done as Mata function or in-line call, using Mata for this purpose beats the old Stata matrix language seven ways from Sunday.

mata: mata clear
void underbar(string scalar mat, real scalar thresh)
	real scalar i
	i = rows(st_matrix(mat))
st_matrix("B", (st_matrix(mat) :< thresh) :/ st_matrix(mat) :* (J(i,i, 1) - I(i)) )

sysuse auto,clear
qui reg price trunk weight length turn
mat list e(V)
mata: underbar("e(V)", 10)
mat list B

Kit Baum, Boston College Economics and DIW Berlin
An Introduction to Modern Econometrics Using Stata:

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index