Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: [Mata] naming matrices in a loop


From   Antoine Terracol <[email protected]>
To   [email protected]
Subject   Re: st: RE: [Mata] naming matrices in a loop
Date   Fri, 19 Mar 2010 08:03:29 +0100

Thanks Nick and Stas,

I've tried pointers, but I must be doing something wrong. Must have a closer look at the manual.

Since the matrices need not have the same dimensions, I cannot just populate a larger matrix with the results.

Antoine

On 18/03/2010 16:52, Stas Kolenikov wrote:
If you REALLY want to do something like that, you can return the
results to Stata and pull them back as you need:

for (n=1;n<=10;n++){
                 ...
                 st_matrix("result"+strofreal(n),<result of the calculation>)
              }

A = st_matrix( "result1") * st_matrix("result2") + st_matrix("result3")

But I totally agree with Nick that you should try to avoid this, and
am learning to write my programs that way :). May be you can write a
function that returns the matrix with the results. May be you can
accumulate the results into a larger matrix (if your results are
vectors rather than matrices). With a more heavy weight machinery of
Mata (and some elegance, too), you might be able to set up a vector of
structures an element of which is a matrix (help
m2_struct##tagmatrices). Then you could address them as
mystruct[k].mymatrix.

Each programming environment has its own strengths that you should try
to utilize. I am always driven nuts when I see Fortran or SAS code
lurking behind a do-file that somebody sends to me :)).

On Thu, Mar 18, 2010 at 10:33 AM, Nick Cox<[email protected]>  wrote:
In a word, pointers.

Another answer: Although experience in Stata can lead you to want to
program in this way, I often find that the problem can be structured to
avoid it.

Nick
[email protected]

Antoine Terracol

There must be an obvious way to solve my problem, but I just seem to be
unable to find it...

In a Mata loop, I want to store the result of each pass into a separate
matrix whose name depends on the value of the counter

It would look like:

for (n=1;n<=10;n++){
                ...
                name_that_depends_on_n=<result of the calculation>
                }

so that I get 10 matrices named, for example, result1, result2, ...,
result10.

In Stata, I would do something like

forvalues i=1/10 {
                ...
                result`i'=<result of the calculation>
                }



How can this be done in Mata?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/





*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index