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

st: re: matrix manipulation

From   "Susan Olivia" <>
Subject   st: re: matrix manipulation
Date   Fri, 12 Dec 2008 07:28:32 -0800

Thanks for the tips, Kit. I would like to use this
opportunity to ask 3 follow up questions:

1) For each cell (i,j) of matrix B, I would like to replace
its value with 1/(A[ij]) instead of dummy [0 1] as in
before. In other words, matrix B[i,j] = 1/A[i,j] if A[i,j] <
THRESHOLD. I seem having problem in doing this.

2) The syntax you suggested "underbar", I seem couldn't find
this entry in the STATA manual. Can you please explain what
is this doing? Perhaps once I know what "underbar" is really
doing, I can tackle #1.

3) Also, when the threshold condition met, the diagonal
element of matrix B is replaced to 1 or 1/aij. Ideally, I
want the diagonal element to be zero. Is there an efficient
way to do this? What I currently do is transform the matrix
into variables and use the loop to change the diagonal
element and then transform the variables into the matrix.

svmat B

local i=1
     while `i'<=_N {
     qui recode B`i' 1=0 in `i'
     local i=`i'+1

mkmat B*, matrix(B)

Thanks in advance for any programming tips.


On Thu, Dec 11, 2008 at 8:00 AM, Kit Baum <>

    Susan said

    I would like to create a binary matrix (say matrix B)
    the existing information from current matrix A.

    Specifically, for each cell (i,j) of matrix B, I want to
    replace its value with 1 if the value of the
    cell (i,j) of matrix A is less than the specified
    value. In other words, matrix B[i,j] = 1 if A[i,j] <

    No subscripting required:

    mata: mata clear
    void underbar(string scalar mat,
                 real scalar thresh)
           st_matrix("B", (st_matrix(mat) :< thresh))

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

    You could pass the name of the result matrix to Mata as
well if you wanted this to be a bit more general.

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

    *   For searches and help try:

*   For searches and help try:

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