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: "Correct" way to convert a vector into a matrix


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: "Correct" way to convert a vector into a matrix
Date   Sun, 25 Dec 2011 11:59:22 +0000

Matt and Scott quite rightly advise using Mata here.

For another historic solution, see -matvec- from STB-56.

STB-56  dm79  . . . . . . . . . . . . . . . . . . Yet more new matrix commands
        (help matcorr, matewmf, matvsort, svmat2 if installed)  . .  N. J. Cox
        7/00    pp.4--8; STB Reprints Vol 10, pp.17--23
        commands to produce a correlation matrix, elementwise monadic
        function of another matrix, selected subsets of matrix rows
        and columns, vec or vech of a matrix, elements sorted within
        a vector, matrix from a vector, and commands to save matrices
        see mata matrix language incorporated into Stata 9.0

and for other such add-ons for Stata matrices

STB-50  dm69  . . . . . . . . . . . . . . . . . .  Further new matrix commands
        (help matdelrc, matewm, matmad, matpow if installed)  . . .  N. J. Cox
        7/99    pp.5--9; STB Reprints Vol 9, pp.29--34
        collection of new matrix commands providing additional matrix
        checking, management, element-wise operators, maximum absolute
        difference, and power

STB-39  dm49  . . . . . . . . . . . . . . . . . . . . Some new matrix commands
        (help matfunc, varfunc if installed)  . . . . . . . . . . .  J. Weesie
        9/97    pp.17--20; STB Reprints Vol 7, pp.43--48
        collection of new matrix commands; several for explicit matrices
        and a few for implicit matrices (i.e., variables)
        see mata matrix language incorporated into Stata 9

Nick

On Fri, Dec 23, 2011 at 4:45 PM, Richard Herron
<richard.c.herron@gmail.com> wrote:
> Thanks, Matt and Scott! I didn't realize it was so easy to move back
> and forth between Mata and Stata.
>
> On Fri, Dec 23, 2011 at 11:35, Scott Merryman <scott.merryman@gmail.com> wrote:
>> You can use mata's -rowshape- :
>>
>> clear*
>> matrix temp_vector = (1, 2, 3, 4, 5, 6, 7, 8, 9)
>> mata: st_matrix("x", rowshape( st_matrix("temp_vector")', 3) )
>> mat list x
>>
>> Scott
>>
>>
>> On Fri, Dec 23, 2011 at 10:21 AM, Richard Herron
>> <richard.c.herron@gmail.com> wrote:
>>> I would like to convert a vector, which I think is really a row matrix
>>> in Stata, into a square matrix. I have a loop solution that works, but
>>> it seems too awkward to be the "correct" approach. Is there a one (or
>>> two) line solution? I found a lot of matrix functions regarding
>>> diagonals, but none that wrap a vector into a matrix.
>>>
>>> In this case -svar- provides the long-run restriction matrix as a
>>> vector where entries are down columns, then across rows.
>>>
>>> * begin code
>>> matrix temp_vector = (1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> matrix temp_matrix = J(3, 3, 0)
>>> foreach i of numlist 1/3 {
>>>    foreach j of numlist 1/3 {
>>>        scalar temp_scalar = `= `j' - 1' * 3 + `i'
>>>        matrix temp_matrix[`i', `j'] = el(temp_vector, 1, temp_scalar)
>>>    }
>>> }
>>> matrix list temp_matrix
>>> * end code

*
*   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