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 Kj�r Hansen <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: RE: How to convert a MATA matrix into a STATA matrix?
Date   Fri, 3 Mar 2006 15:21:47 +0100

Given a defined Mata matrix out, add the line

  st_matrix("output",out) 

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

  st_store(.,1..cols(out),out) 

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

or

  st_store(.,("var1","var2","var3","var4","var5"),out)

to replace specific variables.

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

  st_addobs(rows(out))
  varidx = st_addvar("double", ("var1","var2","var3","var4","var5"))
  st_store(.,varidx,out)

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.

/Jesper


> 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:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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