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]

st: Re: how to change matrix names with each iteration of a loop in Mata


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: how to change matrix names with each iteration of a loop in Mata
Date   Sun, 8 Sep 2013 12:03:28 +0900

Limin Fang wrote:

I am trying to automatically create a number of matrices inside a loop in a Mata
function, and I want the names of the matrices to be associated with each 
iteration index. I am having a lot of trouble with it since I cannot use macro 
in Mata. Here is an example of what I want to achieve:

version 10
mata:
void indx(string scalar varlist, string scalar touse) 
{
	...
	
	k = 1
	
	while (k <= kvar) {
		
		xk = J(k, k, k)

		k++
	}
	...

}
end

Note that in the above script, "xk" stands for x1, x2, x3 .., i.e. in the first
iteration, x1 matrix is created, and in the second one, x2 is created, and so 
on. 
What should I write to achieve this? I noticed that Mata doesn't have an eval()
function as in MatLab, making this very hard to do.

Could anyone possibly help me with this?

--------------------------------------------------------------------------------

Mata has a -stata()- function that you can take advantage of to emulate an
eval() function using Stata local macros.  An example of this that shows the
general method is here:  

http://www.stata.com/statalist/archive/2012-10/msg00920.html

As an alternative, why not just use a pointer vector pointing to the matrices?
It seems a more natural way to work with the Mata language and a more foolproof
way to index your matrices.  It ought to execute more quickly, and be less 
confusing for code review and to debug than call-backs to Stata just to emulate
another programming language's eval().  

The do-file below illustrates the basic approach.

Joseph Coveney


. version 10.2

. 
. mata:
------------------------------------------------- mata (type end to exit)
----------------------------------------------------------------
: mata clear

: mata set matastrict on

: 
: void function indx(real scalar kvar) {
> 
>     pointer(real matrix) vector IndexedMatrices
>     IndexedMatrices = J(1, kvar, NULL)
>     
>     real scalar k
>     for (k=1; k<= kvar; k++) {
>         IndexedMatrices[k] = &J(k, k, k)
>     }
> 
>     // Verification
>     real matrix ReferencedMatrix
>     for (k=1; k<=3; k++) {
>         ReferencedMatrix = *IndexedMatrices[k]
>         printf("%01.0f, %01.0f, %01.0f\n", rows(ReferencedMatrix),
>             cols(ReferencedMatrix), ReferencedMatrix[k, k])
>     }
> }

: 
: indx(3)
1, 1, 1
2, 2, 2
3, 3, 3

: 
: end
--------------------------------------------------------------------------------
----------------------------------------------------------

. 
. exit

end of do-file

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


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