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: Matrix question


From   Christopher Baum <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: re: Matrix question
Date   Sun, 5 Feb 2012 20:11:36 -0500

<>
I have a large mata matrix that I want to reduce: I want to replace each
block of five rows with a single row containing their mean.

My brute force approach goes as follows

| mata:
| t = mean(s[1..5 , ])
| for (i=2; i<=rows(s)/5; i++) {
|   t = t \ mean(s[1+(i-1)*5..5+(i-1)*5 , ])
| }
| end

This works, but am I missing a more efficient way of doing this, some
Mata or matrix-algebra trick?

Something tells me that I should be able to avoid looping here.



Probably some more elegant way of generating the transformation matrix, but:

mata
bigmat = runiform(15,6)
trans = J(1, 5, 1/5)
zer = J(1,5,0)
transmat = (trans, zer, zer \ zer, trans, zer \ zer, zer, trans)
reduced = transmat * bigmat
bigmat
reduced
end


Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
                             An Introduction to Stata Programming  |   http://www.stata-press.com/books/isp.html
  An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html


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