Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

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


From   Stas Kolenikov <skolenik@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: [Mata] naming matrices in a loop
Date   Thu, 18 Mar 2010 10:52:34 -0500

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 <n.j.cox@durham.ac.uk> 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
> n.j.cox@durham.ac.uk
>
> 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/
>



-- 
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.

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