Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: RE: How to convert a MATA matrix into a STATA matrix?

From   Jesper Kjr Hansen <>
To   <>
Subject   st: RE: RE: RE: How to convert a MATA matrix into a STATA matrix?
Date   Fri, 3 Mar 2006 15:14:27 +0100

Given a defined Mata matrix out, add the line


at the end of your Mata code. Then you will have your Mata matrix as a Stata matrix
(verify with -matrix list output- once you get back in Stata).

Alternatively, you could also output to a Stata data set. To store matrix contents in
a number of variables, which are already defined (in the data set loaded into
memory), use


to replace the first k variables in the Stata data set with the contents of matrix
out [k = cols(out)]



to replace specific variables.

If you don't have a data set loaded in memory, you have to define the dimensions

   varidx = st_addvar("double", ("var1","var2","var3","var4","var5"))

You can use temporary variables like this instead:

   varidx = st_addvar("double", st_tempname(cols(out)))

This may be convenient, if you don't know the number of cols in matrix out
beforehand; but then you have to rename varibles once you're back in Stata (temp
variables are cleared, once your do file is finished)

   qui describe
   local nvars = `r(k)'
   unab varlist: _all
   local count 1
   foreach var of local varlist {
       rename `var' var`count++'

I hope this helps.


> Hej Jesper,
> hm, I still don't know how that would work. My Mata code is
> mata
> m_all 	= st_matrix("all")
> nom	= m_all[1...,2]
> denom	= m_all[1...,3]
> denom	= (denom:/denom)-denom
> lr 	= nom :/ denom
> out	= m_all[1...,1], lr, m_all[1...,4], m_all[1...,5]
> end
> and I'd like to turn "out" into data format.
> Any ideas?
> Uli
>> can anybody tell me how to convert a matrix generated in MATA
>> into STATA format?
>> What I am looking for is hence the reverse of st_matrix.
>> What I would like to ultimately do is to convert my MATA
>> matrix into data (like "svmat" for STATA matrics).
>> Best regards,
>> UK

*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index