UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             
Loading

Stata FAQ
How do I do elementwise operations on a matrix?

Here is a small (3x3) Stata example of a matrix in which each element is exponentiated.  You can generalize this example to any kind of elementwise matrix operation.
matrix x = (1,2,3\2,3,4\3,4,5)
matrix list x

symmetric x[3,3]
    c1  c2  c3
r1   1
r2   2   3
r3   3   4   5

matrix y = x /* create y */

/* begin loop */
local i=1
while `i'<=3 {
  local j=1
  while `j'<=3 {
    matrix y[`i' , `j'] = exp(x[`i' , `j'])
    local j = `j' + 1
  }
   local i = `i' + 1
} /* end loop */

/* matrix y is now the exponentiated matrix */
matrix list y

symmetric y[3,3]
           c1         c2         c3
r1  2.7182818
r2  7.3890561  20.085537
r3  20.085537   54.59815  148.41316

How to cite this page

Report an error on this page or leave a comment

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.