Statalist The Stata Listserver


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

st: macros & expanding names in mata?


From   Matissa Hollister <m73hollis@yahoo.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: macros & expanding names in mata?
Date   Mon, 6 Mar 2006 08:02:09 -0800 (PST)

I'm still trying to figure out how to think in mata
terms after finally getting comfortable in stata.  I
struggle in particular with how to do things in mata
that I find relatively easy to do in stata with
macros.  Right now I'm stumped with the following
question.

I have a set of matrices I created in mata with
distances between pairs of individuals.  The algorithm
I used to calculate the distances varies by two
parameters, x and y, each ranging from 1 to 100.  Each
matrix represents the results under different levels
of these two parameters and the matrix names reflects
these settings (e.g. x10y20 or x20y40).  I now want to
do a series of manipulations and calculations on these
matrices and I'm having difficulties figuring out how
to iterate across the matrix names, in particular how
to proxy for the names.  Here's an example.  The
original distance matrices are already loaded into
memory.

//parameters is a list of the different parameter
settings, corresponding to the existing matrices
parameters=(10,20\10,30\20,40)

//create submatrices for each of the distance matrices

for (i=1; i<=rows(parameters); i++) {
	x=parameters[i,1]
	y=parameters[i,2]
	
	sub_x`x'y`y'=x`x'y`y'[|1,1\20,20|]  
}

The last line is obviously wrong, but it reflects what
I can't figure out in mata terms.  Basically, I want
to create:
	sub_x10y20=x10y20[|1,1\20,20|]
	sub_x10y30=x10y30[|1,1\20,20|]
	sub_x20y40=x20y40[|1,1\20,20|]

Later on I want to do other calculations for each of
the matrices.  I know that pointers are in some ways
the equivalent to macro in mata, although I'm still
trying to wrap my head around them.  It seems like
macros are more flexible in their ability to expand
within strings and variable names.  I know, though,
that some things just have to be done differently
within mata because it's compiled, so perhaps I'm
going at this all wrong.  Any suggestions on how to
accomplish this task within mata would be greatly
appreciated.  I could resort to calling things from
stata so that I can use macros, but it would be kind
of a pain.

Thanks,

Matissa

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
*
*   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–2020 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index