Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: sqrt of all elements of a matrix


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: sqrt of all elements of a matrix
Date   Thu, 17 Nov 2005 11:54:39 -0000

By the way, I'd agree with anyone who 
said that the syntax of -matewmf- was
fairly horrible. If I were writing 
it today, I would have implemented 
a different syntax. But there's no 
point in a radical rewrite, given Mata. 

Nick 
n.j.cox@durham.ac.uk 

Nick Cox
 
> Mata Answers This Admirably! 
> 
> . mata :
> ------------------------------------------------- mata (type 
> end to exit) ------------------
> : m = (1,2,3\4,5,6\7,8,9)
> 
> : sqrt(m)
>                  1             2             3
>     +-------------------------------------------+
>   1 |            1   1.414213562   1.732050808  |
>   2 |            2   2.236067977   2.449489743  |
>   3 |  2.645751311   2.828427125             3  |
>     +-------------------------------------------+
> 
> Otherwise, in Stata <9, this is essentially the same question
> as one asked a few days ago. An ancient program 
> to do this, and other _e_lement_w_ise _m_onadic 
> _f_unction problems, is -matewmf-: 
> 
> STB-56  dm79  . . . . . . . . . . . . . . . . . . Yet more 
> new matrix commands
>         (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
> 
> That name is so unlikely that you would never guess it, but 
> 
> . search matrix 
> 
> would have pointed to it. 
> 
> . matrix A = (1,2,3\4,5,6\7,8,9)
> 
> . matewmf A B , function(sqrt)
> 
> . mat li B 
> 
> B[3,3]
>            c1         c2         c3
> r1          1  1.4142136  1.7320508
> r2          2   2.236068  2.4494897
> r3  2.6457513  2.8284271          3
> 
> -matewmf- as published in STB-56 is about 
> 30 lines long, but can now be written much 
> more concisely: 
> 
> *! 1.1.0  NJC 17 November 2005
> *! 1.0.0  NJC 25 January 2000
> program matewmf 
> 	version 8.0
> 	gettoken A 0 : 0
> 	gettoken B 0 : 0, parse(" ,") 
> 	syntax , Function(str) 
> 	tempname C 
> 	mat `C' = `A'  
> 	forval i = 1/`=rowsof(matrix(`A'))' {
>         	forval j = 1/`=colsof(matrix(`A'))' { 
> 			mat `C'[`i',`j'] = `function'(`A'[`i',`j'])
> 	        }
> 	}
> 	mat `B' = `C' /* allows overwriting of either `A' or `B' */
> end
> 
> from which you can see, as would be expected, that
> the problem boils down to a double loop. 
> 
> Nick 
> n.j.cox@durham.ac.uk 
> 
> I.A.C. van de Snepscheut
>  
> > Is there a simple way to take the sqrt of all the elements in 
> > a matrix?

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index