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]

st: Re: st: Matrices utilities, Basic calculations. When to start using Mata over Stata?‏


From   Austin Nichols <[email protected]>
To   [email protected]
Subject   st: Re: st: Matrices utilities, Basic calculations. When to start using Mata over Stata?‏
Date   Fri, 23 Apr 2010 09:58:47 -0400

Radaf paf <[email protected]>:
You can loop in Stata and use Mata, or you can do rowsums by
postmultiplying by a column vector of ones.
e.g.

forv n=1/10 {
 mata: st_matrix("a`n'",runiform(25,36))
 mata: st_matrix("b`n'",runiform(25,36))
 mat c`n'=J(25,1,.)
}
mat li a10
forv n=1/10 {
 mata: a=st_matrix("a`n'")
 mata: b=st_matrix("b`n'")
 mata: ra=a*J(cols(a),1,1)
 mata: rb=b*J(cols(a),1,1)
 mata: c=ra:*(ra:>rb)
 mata: st_matrix("c`n'",c)
}
forv n=1/10 {
 mata: ra=rowsum(st_matrix("a`n'"))
 mata: rb=rowsum(st_matrix("b`n'"))
 mata: c=ra:*(ra:>rb)
 mata: st_matrix("d`n'",c)
}
mat li c10
mat li d10


2010/4/23 Radaf paf <[email protected]>:
> Hi all,
>
> I have a total of 20 matrices, A1-A10 and B1-B10 of size
> 25x36
>
> I am trying to do the sum of each row of the matrix A and
> compare them with the sum of each row of the matrix B. Then I retain the
>  highest sum and put it in a new matrix, say C.
>
> The "rowsum" utility in Mata is appealing, but I couldn't figure out how to create
> the loops ( no "foreach" command?), generate new matrixes names
> automatically (for the matrices C) and access my 20 matrices within the
> loops in Mata.
>
> Here is what I would do if I could use "rowsum"
> in Stata:
>
>
> foreach n in 1 2 3 4 5 6 7 8 9 10 {;
>
>
>  matrix c'n´= J(rowsof(A'n´),1,0)     *I create my matrix c (vector of
> 25 rows) containing only 0 values ;
>
>     foreach i in 1-25 {;
>         * where i correspond to the number of rows;
>
>
>
>  R_sum_A'n´=rowsum(A'n´) ;
>         R_sum_B'n´=rowsum(B'n´) ;
>
>
>
>       *I compare the two sums and if the rowsum of A is bigger than B I
> put it in the new matric C, if not I leave it equals to 0 ;
>
>
>      if el(R_sum_A'n´,'i´,1)> el(R_sum_B'n´,'i´,1)
>
>
>      matrix C'n´['i´,1] =el(R_sum_A'n´,'i´,1) ;
>
>
>
>
>  };
>  };
>
>
> But I cannot use "rowsum" into stata . So what would you suggest ?
>
> Thanks a lot
>
> Raphael

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