 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Filling mata results in a matrix

 From Christophe Kolodziejczyk To "statalist@hsphsun2.harvard.edu" Subject Re: st: Filling mata results in a matrix Date Mon, 7 Oct 2013 17:42:26 +0200

```Dear Jonas
The matrix V is defined outside of your function, therefore it doen't
exist in sub_matriy. One solution is to pass it as an argument.
Best
Christophe

mata
void sub_matriy(real matrix V)
{
D = st_matrix("X")
r = st_numscalar("row_mata")
o = st_numscalar("columns_mata")

V[r,o] = mean(select(vech(D), vech(D) :< 1))
}
mata mosave sub_matriy(), replace
end

2013/10/7 Sobott, Jonas <jonas.sobott@outlook.de>:
> Dear Statalist,
>
> I have a problem with filling results from a mata function into matrix. I
> want to loop over 6 regions and 10 sectors, perform for each a mata function
> (see above) and store the result in a matrix. To do so, I created to scalars
> from the loop variables (row_mata and column_mata) to tell mata where to put
> the results.
>
> However, all I get is the sub_matriy():  3301  subscript invalid, <istmt>:
> -  function returned error
>
>
> clear all
> mata
> V = J(6,10,.)
> end
>
> forvalues j = 1/6 {
>
> forvalues i=1/10{
> clear
> use master2
> scalar columns_mata = `i'
> scalar row_mata = `j'
> quietly: drop if missing(sector_num)
> quietly: keep if sector_num == `i'
> quietly: xtset entity_num date, daily delta(7)
> quietly: keep entity_num sector_num date  newtrades
> quietly: reshape wide  newtrades, i(entity_num) j(date)
> quietly: xpose, clear
> quietly: drop if v1 + v2 == 2* `i'
> quietly: recode _all (missing =0)
> quietly: corr _all
> quietly: matrix X = r(C)
> mata sub_matriy()
> }
> }
>
>
> with
>
> mata
> void sub_matriy()
> {
> D = st_matrix("X")
> r = st_numscalar("row_mata")
> o = st_numscalar("columns_mata")
> V[r,o] = mean(select(vech(D), vech(D) :< 1))
> }
> mata mosave sub_matriy(), replace
> end
>
>
> Thank you very much!
>
> Best regards
> Jonas Sobott
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

--
Christophe Kolodziejczyk
Research Fellow

AKF, Anvendt KommunalForskning
Danish Institute of Governmental Research