Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: re: Matrix question

From   Christopher Baum <>
To   "" <>
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:

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

Kit Baum   |   Boston College Economics & DIW Berlin   |
                             An Introduction to Stata Programming  |
  An Introduction to Modern Econometrics Using Stata  |

*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index