Statalist The Stata Listserver


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

Re: st: absolute values from a matrix


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: absolute values from a matrix
Date   Thu, 9 Nov 2006 23:53:17 -0600

On Nov 9, 2006, at 8:59 AM, Tom Boonen wrote:
is there an elegant way to create a matrix that contains the absolute values of the entries of another matrix?

Mata is elegant. For example, Mata's scalar functions (like abs()) will all operate on matrices element-wise. Thus, to use your example (from the Mata prompt):


: foo = (1, -1 \ 2, -2)

: abs(foo)
1 2
+---------+
1 | 1 1 |
2 | 2 2 |
+---------+


Type -help mata- to learn more. If you prefer instead to work within Stata, you can use a single Mata call to do what you want, like this:


. mat foo = (1, -1 \ 2, -2)

. mata: st_matrix("absfoo",abs(st_matrix("foo")))

. mat li absfoo

absfoo[2,2]
c1 c2
r1 1 1
r2 2 2


-- Phil

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




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