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]

Re: st: "Correct" way to convert a vector into a matrix


From   Matthew White <[email protected]>
To   [email protected]
Subject   Re: st: "Correct" way to convert a vector into a matrix
Date   Fri, 23 Dec 2011 11:34:11 -0500

Hi Richard,

Looks like a job for Mata to me:

matrix temp_vector = (1, 2, 3, 4, 5, 6, 7, 8, 9)
mata: st_matrix("temp_matrix", rowshape(st_matrix("temp_vector"),
sqrt(cols(st_matrix("temp_vector"))))')
matrix list temp_matrix

Best,
Matt

On Fri, Dec 23, 2011 at 11:21 AM, Richard Herron
<[email protected]> 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
>
> Thanks!
> *
> *   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/



-- 
Matthew White
Data Coordinator
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org

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