Statalist


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

st: RE: How can I get square roots of elements in a matrix,in Stata8.2?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: How can I get square roots of elements in a matrix,in Stata8.2?
Date   Tue, 28 Aug 2007 13:26:59 +0100

A -search- yields sources of advice: 

FAQ     . . . . . . . . . Performing element-by-element operations on matrices
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  A. McDowell
        4/05    How do I perform element-by-element operations on
                matrices?
                http://www.stata.com/support/faqs/data/matrix.html

FAQ     . . . . . . . . . . .  How do I do elementwise operations on a matrix?
        . . . . . . . . . . . . . . . . . .  UCLA Academic Technology Services
        10/06   http://www.ats.ucla.edu/stat/stata/faq/elemmatrix.htm

but there is more to be said. 

The easiest solution in terms of your typing is to install -matmap- from
SSC. 

matmap A B, map(sqrt(@))

creates B which contains the roots of the elements of A. 

This is almost as easy and will develop understanding: 

mat B = A 
forval i = 1/`= rowsof(A)' { 
	forval j = 1/`= colsof(A)' { 
		mat B[`i', `j'] = sqrt(A[`i, `j']) 
	}
} 

The UCLA FAQ above does this kind of thing using -while-. That
is here more cumbersome than using -forval-. 

Here is -matmap-. 

---------------------------------------------------
program define matmap 
*! 1.0.0  NJC 23 August 2000
        version 6.0
        gettoken A 0 : 0
        gettoken B 0 : 0, parse(" ,") 
        syntax , Map(str) [ Symbol(str) ]

        if "`symbol'" == "" { local symbol "@" } 

        if !index("`map'","`symbol'") { 
                di in r "map( ) does not contain `symbol'" 
                exit 198 
        }       
        
        local nr = rowsof(matrix(`A'))
        local nc = colsof(matrix(`A'))
        
        tempname C val 
        mat `C' = `A'  

        local i 1
        while `i' <= `nr' {
                local j 1
                while `j' <= `nc' {
                        local exp : /* 
                        */ subinstr local map "`symbol'" "`A'[`i',`j']", all 
                        scalar `val' = `exp' 
                        if `val' == . {
                                di in r "matrix would have missing values"
                                exit 504
                        }
                        mat `C'[`i',`j'] = `val' 
                        local j = `j' + 1
                }
                local i = `i' + 1
        }

        mat `B' = `C' /* allows overwriting of either `A' or `B' */
end
--------------------------------------------------

Here is how, 7 years on, I would rewrite it for Stata 8.2 [sic]. 
Not tested! 

1. -forval- is nicer than -while-. 
2. Stata's matrices can now include missing values. 
3. Some other small changes.

So all the program does is set up two loops over
the rows and columns and do your stuff elementwise. 

----------------------------------------------- matmap8 
*! 1.1.0  NJC 28 August 2007 
program matmap8 
        version 8.2
        gettoken A 0 : 0
        gettoken B 0 : 0, parse(" ,") 
        syntax , Map(str) [ Symbol(str) ]

        if "`symbol'" == "" local symbol "@" 

        if !index("`map'","`symbol'") { 
                di as err "map() does not contain `symbol'" 
                exit 198 
        }       
        
        local nr = rowsof(matrix(`A'))
        local nc = colsof(matrix(`A'))
        tempname C 
        mat `C' = `A'  

        forval i = 1/`nr' {
                forval j = 1/`nc' {
                        local exp : /* 
                        */ subinstr local map "`symbol'" "`A'[`i',`j']", all 
                        mat `C'[`i',`j'] = `exp'
                }
        }

        mat `B' = `C' /* allows overwriting of either `A' or `B' */
end
------------------------------------------------------

Nick 
[email protected] 

Xiaoheng Zhang
 
> I am looking for a command or program to transfer a matrix into a new
> matrix with each element becoming its square root. How can I do this
> in Stata 8.2?Can I do it in Stata 9.0 with mata?But currently I do not
> have Stata 9.0.Thank you.

*
*   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