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: Mata data question


From   tzygmund mcfarlane <tzygmund@googlemail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Mata data question
Date   Mon, 1 Aug 2011 10:51:30 -0700

The syntax for -spmat putmatrix- (part of -sppack-, by Drukker, Peng,
Prucha, and Raciborski, SSC) is:

spmat putmatrix objname matname [vecname] [, options]

And although the Mata code is only available as compiled library, I
would guess that the object that -spmat putmatrix- saves is
essentially a Mata matrix with frills. So, issuing the replace option
clears any previous matrix with given "objname" (in this case, "G")
first and then tries to save "matname" (in this case, also "G") to
that location.

So for example, this code will work:

/****************************************/
clear*
mata
void blkdiag(real matrix gr, real matrix G_in, real matrix G_out) {
	if (G_in == .) G_out = gr
	else G_out = blockdiag(G_in, gr)
}
end

mata: blkdiag(J(3,3,1)-I(3),G=.,G)
mata: blkdiag(J(2,2,1)-I(2),G,G)
mata: blkdiag(J(4,4,1)-I(4),G,G)
mata: G

spmat putmatrix newobj G, replace
spmat summarize newobj
/****************************************/


On Mon, Aug 1, 2011 at 10:25 AM, Benjamin Allaire <btallaire@hotmail.com> wrote:
> Hi -
>
> I am a bit confused about what exactly mata keeps in memory.  Why doesn't this code work? Why does G go away?
>
> Thanks in advance!
>
> Ben
>
> .
> . mata:
> ------------------------------------------------- mata (type end to exit) ------------------------------------------------------------------------------------------------------------------------------
> :         void blkdiag(real matrix gr, real matrix G_in, real matrix G_out) {
>>                         if (G_in == .) G_out = gr
>>                         else G_out = blockdiag(G_in, gr)
>>         }
>
> : end
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> .
> .
> . mata: blkdiag(J(3,3,1)-I(3),G=.,G)
>
> . mata: blkdiag(J(2,2,1)-I(2),G,G)
>
> . mata: blkdiag(J(4,4,1)-I(4),G,G)
>
> .
> . mata: G
> [symmetric]
>        1   2   3   4   5   6   7   8   9
>     +-------------------------------------+
>   1 |  0                                  |
>   2 |  1   0                              |
>   3 |  1   1   0                          |
>   4 |  0   0   0   0                      |
>   5 |  0   0   0   1   0                  |
>   6 |  0   0   0   0   0   0              |
>   7 |  0   0   0   0   0   1   0          |
>   8 |  0   0   0   0   0   1   1   0      |
>   9 |  0   0   0   0   0   1   1   1   0  |
>     +-------------------------------------+
>
> .
> . spmat putmatrix G G, replace
>                  <istmt>:  3499  G not found
> r(3499);
>
>
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index