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

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

 From Tirthankar Chakravarty To statalist@hsphsun2.harvard.edu Subject st: Re: st: Matrices utilities, Basic calculations. When to start using Mata over Stata?‏ Date Fri, 23 Apr 2010 19:22:54 +0530

```Here is a solution using Mata. Note that line 12 of this code will
wrap in this email. Ensure that in your do-file "mata: mCtemp[...]" to
"[...]rowsum(st_matrix("B`i'")))" is one contiguous line.  Avoiding
this will require a little more work, but can be done. Also, you will
need to install Ben Jann's -moremata- package.
******************************************
clear*
clear mata
forv i=1/20 {
matrix A`i' = matuniform(25,36)
matrix B`i' = matuniform(25,36)
matrix C`i' = J(25, 1, 0)
}

forv i=1/20 {
// need Ben Jann's -moremata- package
mata: mCtemp = mm_cond(rowsum(st_matrix("A`i'")):>
rowsum(st_matrix("A`i'")), rowsum(st_matrix("A`i'")),
rowsum(st_matrix("B`i'")))
mata: st_replacematrix("C`i'", mCtemp)
}
******************************************

> 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
> _________________________________________________________________
> Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
> https://signup.live.com/signup.aspx?id=60969
> *
> *   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).

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