# Re: st: Making a Matrix from Three Variables

 From Tirthankar Chakravarty To statalist@hsphsun2.harvard.edu Subject Re: st: Making a Matrix from Three Variables Date Sat, 9 May 2009 22:39:38 +0100

```<>
<>
Many apologies. Read Glenn for Allan.

T

On Sat, May 9, 2009 at 10:38 PM, Tirthankar Chakravarty
<tirthankar.chakravarty@gmail.com> wrote:
> <>
> <>
> 1) Amadou's solution can be streamlined considerably by replacing the
> first part as in the code included below.
>
> 2) The line:
> tomata x y z, missing
> does not work because -missing- is not a valid option for -tomata- (Gould, SSC).
>
> 3) Allan's solution is efficient, but you might want to watch out for
> missing values in your original data. So, a slight modification is
> included below.
>
> /*** Begin ***/
> /* Simulate some data */
> clear
> clear mata
> set obs 100
> // create a 20x5 matrix
> egen cols = seq(), from(1) to(5)
> sort cols
> egen rows = seq(), from(1) to(20)
> gen var3 = runiform()
>
> /* Mata solution - I  - not flexible */
> sort cols rows /* note sort order */
> qui su rows
> scalar define sMvrows = r(max)
> qui su cols
> scalar define sMvcols = r(max)
> mata:
> vA = st_data(.,"var3")
> mB = colshape(vA,st_numscalar("sMvrows"))'
> mB
> end
>
> /* Mata solution - II  - Allan's solution modified */
> mata:
> real matrix sparse(real matrix x)
> {
>  real matrix y
>  real scalar k
>
>  y = J(colmax(x[,1]),colmax(x[,2]),.)
>  for (k=1; k<=rows(x); k++) {
>    y[x[k,1],x[k,2]] = x[k,3]
>  }
>
>  return(y)
> }
> mC = st_data(.,("rows", "cols", "var3"))
> mD = sparse(mC)
> mD
>
> /* Check the two solutions are equivalent */
> asserteq(mD, mB)
> end
>
> /*** End ***/
>
> On Sat, May 9, 2009 at 7:13 PM, Amadou DIALLO <stata.diallo@gmail.com> wrote:
>> Hi Allan,
>>
>> This is my 3 cents solution (have not get the time to put it in a
>> formal mata programm, just tested on the command line). I hope it can
>> lead you to what you need to achieve if you play around a bit with it.
>>
>> Best regards.
>>
>>
>>
>> clear
>>
>> // suppose you have 3 variables x, y, z, with number of observations
>> of x+y = number of observations of z
>>
>> set obs 10
>>
>> g x=.
>> replace x=1 in 1
>> replace x=2 in 2
>>
>> g y=.
>> replace y =1 in 1
>> replace y =2 in 2
>> replace y =3 in 3
>> replace y =4 in 4
>> replace y =5 in 5
>>
>> //g z = round(uniform(),1)
>>
>> g z=.
>> replace z =1 in 1
>> replace z =2 in 2
>> replace z =3 in 3
>> replace z =4 in 4
>> replace z =5 in 5
>> replace z =6 in 6
>> replace z =7 in 7
>> replace z =8 in 8
>> replace z =9 in 9
>> replace z =10 in 10
>>
>> // tomata  x y z, missing // Don't know why not working
>> tomata x in 1/2
>> tomata y in 1/5
>> tomata z
>>
>> mata:
>>
>> c = rows(x)
>> d = cols(y')
>> M=J(c,d,.)
>> for(i=1;i<=d;i++) {
>>   M[1,i] = z[i]
>>   k=d+i
>>   M[c,i] = z[k]
>> }
>> M
>> z
>> end
>>
>>
>>
>> 2009/5/9, Glenn Goldsmith <glenn.goldsmith@gmail.com>:
>>> Hi Allan,
>>>
>>> It seems like what you're after is more-or-less the same as converting from
>>> sparse matrix storage, so the mata code here should do the trick:
>>>
>>> http://www.stata.com/statalist/archive/2009-04/msg00142.html
>>>
>>> You just need to get your variables into a mata matrix to start with and you
>>> should be away:
>>>
>>>   mata : st_view(x=.,.,("var2","var1","var3"))
>>>
>>> NB: Note the inverted order of var1 and var2. This is because the code for
>>> -sparse()- assumes that the first variable provides the row index, and the
>>> second variable provides the column index.
>>>
>>> HTH,
>>>
>>> Glenn.
>>>
>>> Allan Joseph Medwick <amedwick@gmail.com> wrote:
>>>
>>> Hi, All,
>>>
>>> I have three variables (var1, var2, var3).  I would like to create a
>>> matrix where the values of var1 are the columns (ascending), the
>>> values of var2 are the rows (also ascending), and the values of var3
>>> are the elements in the matrix.  I know there must be a user defined
>>> procedure out there to do this, but I haven't been able to find it.
>>>
>>> Thanks,
>>> Allan
>>>
>>>
>>> *
>>> *   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/
>>>
>>
>>
>> --
>> ---
>>
>> Development Economist
>> Director, Center for Research and Training on Adult Education
>> Mayotte, FRANCE
>> www.aprosasoma.org
>> +262639693250
>> *
>> *   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/
>>
>
>
>
> --
> To every ω-consistent recursive class κ of formulae there correspond
> recursive class signs r, such that neither v Gen r nor Neg(v Gen r)
> belongs to Flg(κ) (where v is the free variable of r).
>

--
To every ω-consistent recursive class κ of formulae there correspond
recursive class signs r, such that neither v Gen r nor Neg(v Gen r)
belongs to Flg(κ) (where v is the free variable of r).

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