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

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

 From Tom Trikalinos To statalist@hsphsun2.harvard.edu Subject Re: st: RE: [Mata] naming matrices in a loop Date Fri, 19 Mar 2010 08:27:13 -0400

```create a vector of pointers -- each can point to a matrix of different
dimensions.
indexing the vector would point to the matrix you need each time.

If you are familiar with C -- it's the same concept with simpler syntax.

otherwise, it needs a bit of time to get in that frame of mind.

t
On Fri, Mar 19, 2010 at 3:03 AM, Antoine Terracol
<terracol@univ-paris1.fr> wrote:
> 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<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/
>>>
>>
>>
>>
>
> *
> *   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/
```