[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: element wise division of matrices

From   "Nick Cox" <>
To   <>
Subject   st: RE: element wise division of matrices
Date   Wed, 30 Jul 2008 21:30:09 +0100

Possibly more people know about Mata now than about Stata's old matrix
language, so I'll point out that elementwise division of matrices is
possible in Stata using -matewd- from STB-50: 

STB-50  dm69  . . . . . . . . . . . . . . . . . .  Further new matrix
        (help matdelrc, matewm, matmad, matpow if installed)  . . .  N.
J. Cox
        7/99    pp.5--9; STB Reprints Vol 9, pp.29--34
        collection of new matrix commands providing additional matrix
        checking, management, element-wise operators, maximum absolute
        difference, and power

See also 

STB-39  dm49  . . . . . . . . . . . . . . . . . . . . Some new matrix
        (help matfunc, varfunc if installed)  . . . . . . . . . . .  J.
        9/97    pp.17--20; STB Reprints Vol 7, pp.43--48
        collection of new matrix commands; several for explicit matrices
        and a few for implicit matrices (i.e., variables)

STB-56  dm79  . . . . . . . . . . . . . . . . . . Yet more new matrix
        (help matcorr, matewmf, matvsort, svmat2 if installed)  . .  N.
J. Cox
        7/00    pp.4--8; STB Reprints Vol 10, pp.17--23
        commands to produce a correlation matrix, elementwise monadic
        function of another matrix, selected subsets of matrix rows
        and columns, vec or vech of a matrix, elements sorted within
        a vector, matrix from a vector, and commands to save matrices

I've unlimited respect for Mata, but it is not obvious to me that it is
a better language for quite what you are doing than Stata itself -- and
I don't mean Stata's matrix language either. Presumably your rows and
columns have identities that have meaning to you. Looking at 400 X 400
matrices of such a kind is not easy in Mata. 

If you represent your matrices by variables, 

Row identifier 
Col identifier 
Cell values for matrix 1
Cell values for matrix 2

Then elementwise division is naturally just division and most other
basic analyses will be equally trivial. 


Victor, Jennifer Nicoll

Thanks to Nick for recommending a running sum of matrices for my problem
yesterday.  I worked like a dream. Now I have a new problem.

I have created two matrices in Stata using the matrix commands.  One is
called "rawsum," a 438x1010 matrix.  The other is "bothvoting," also a
438x1010 matrix.

I need to create matrix agreecount=(rawsum+bothvoting)/2. No problem.

Now I need to create matrix agreerate=agreecount/bothvoting, where each
i,j of 'agreecount' is divided by each i,j of 'bothvoting'.  I cannot
find a command in Stata's matrix commands to do this type of
element-wise division.

So, I've moved into Mata.  Mata has a simple element-wise division
command: X=A:/B.

But, I am a novice Mata-user and I cannot figure out how to get my
438x438 matrix into Mata.  So, I've converted it back to a dataset using
the 'svmat' command and I'm trying to get the data into Mata using the
'st_view' command.  My data has 438 observations and 438 variables.
Essentially, I want to use the command:
st_view(x., ., "agreecount1",..."agreecount438")

...then do the same with the 'bothvoting' data, then execute the e-w
division command.

I've tried using the 'for' command but cannot figure out the successful
execution of a loop in Mata because the syntax is different than

How can I do this?  Or can you provide a more elegant solution?

*   For searches and help try:

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