Elementwise matrix muliplication in stata 8

300 views
Skip to first unread message

Joseph

unread,
May 6, 2006, 12:51:38 PM5/6/06
to Stata Users Forum
Does anyone know how to do element wise muliplication in Stata 8 matrix
environment. I know that the Mata language in Stata 9 does it, but the
.* syntax does not work in stata 8. How annoying!!

Alan Riley

unread,
May 6, 2006, 7:47:59 PM5/6/06
to Stata-Us...@googlegroups.com, Joseph
Joseph (jacu...@gmail.com) asked about element-wise matrix
multiplication in Stata 8:

> Does anyone know how to do element wise muliplication in Stata 8 matrix
> environment. I know that the Mata language in Stata 9 does it, but the
> .* syntax does not work in stata 8. How annoying!!

The following message in the Statalist archives addresses this and
points at a FAQ on the subject which covers both Stata 9 and Stata 8
methods for element-wise matrix operations:

http://www.stata.com/statalist/archive/2002-07/msg00554.html


Alan
(ari...@stata.com)

Joseph

unread,
May 7, 2006, 6:44:13 PM5/7/06
to Stata Users Forum
The FAQs seem only to confrim what I suspected. There seem to be NO
element wise operators in the Stata 8 matrix environment. Of couse you
can program the operations as shown in the code below, but if your code
uses simple element wise multiplication or division repeatedly then
looping becomes a royal pain.

Code:
To perform element wise division as in Matlab such that C=A./B
wher A and B are 2x5 matrices then you must program as follows.

forvalues i = 1/2 {
forvalues j = 1/5 {
matrix C[`i',`j']= A[`i',`j']/B[`i',`j']
}
}

As the dimension of the matrices increases so do the number of loops.
Not difficult to do if you have to, but a very clunkly solution. I have
heard that they fixed this deficeit in the Mata environment of Stata 9.

Reply all
Reply to author
Forward
0 new messages