Hi list! I have a large matrix which I'd like to partition into blocks and for each block I'd like to compute the mean. Following a example where each letter marks a block of the partition: a a a d g g a a a d g g a a a d g g b b b e h h b b b e h h c c c f i i I'm only interested in the resulting matrix of means. How can this be done efficiently? Thanks! Titus
On Mon, Feb 16, 2009 at 01:45:52PM -0500, Stavros Macrakis wrote:> How are the blocks defined? As a priori index ranges? By factors? By > some property of i,j? Or...?Ok, I should have been more specific. The blocks are defined by factors. There's a factor for the columns and a factor for the rows. In the example below the column factor would be c(1,1,1,2,3,3) and the row factor c(1,1,1,2,2,3). In the particular case I'm working on the matrix is square and symmetric and there's only one factor for both. I can figure out ways to subset the matrix, similar to what Jorge proposed, but I'm looking for a way to get the means more or less at once because the matrix is pretty large and doing it block-wise is too slow. Thanks again! Titus> On 2/16/09, Titus von der Malsburg <malsburg at gmail.com> wrote: > > > > Hi list! I have a large matrix which I'd like to partition into blocks > > and for each block I'd like to compute the mean. Following a example > > where each letter marks a block of the partition: > > > > a a a d g g > > a a a d g g > > a a a d g g > > b b b e h h > > b b b e h h > > c c c f i i > > > > I'm only interested in the resulting matrix of means. How can this be > > done efficiently? > > > > Thanks! Titus > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > >
Assuming your matrix is: mm <- matrix(runif(6*6),6,6) And your blocks are defined by integers or factors: cfact <- c(1,1,1,2,3,3) rfact <- c(1,1,1,2,2,3) Then the following should do the trick: matrix(tapply(mm, outer(rfact,cfact,paste), mean), length(unique(rfact))) The 'outer' calculates a joint factor for each element of the matrix; the 'tapply' treats the matrix as a vector, grouping by factor and calculating means; the 'matrix' rearranges them as a matrix corresponding to the original block structure. Is that what you had in mind? -s On Mon, Feb 16, 2009 at 12:43 PM, Titus von der Malsburg <malsburg@gmail.com> wrote:> > Hi list! I have a large matrix which I'd like to partition into blocks > and for each block I'd like to compute the mean. Following a example > where each letter marks a block of the partition: > > a a a d g g > a a a d g g > a a a d g g > b b b e h h > b b b e h h > c c c f i i > > I'm only interested in the resulting matrix of means. How can this be > done efficiently? > > Thanks! Titus > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Its not clear that the object returned from such an operation would be a matrix, but if things remain very regular then perhapos you will succeed with this: > markmtx <- matrix(scan(textConnection("a a a d g g + a a a d g g + a a a d g g + b b b e h h + b b b e h h + c c c f i i"), what="character"), nrow=6) Read 36 items > nummtx <-matrix(rnorm(36),nrow=6) > nummtx [,1] [,2] [,3] [,4] [,5] [,6] [1,] -1.49492952 -0.1000962 -0.54546587 -0.536216056 0.1065169 -1.3368842 [2,] -0.64393278 0.3343573 0.76247880 0.282666215 0.2236401 0.8210809 [3,] 1.42879752 -1.3246770 0.06403316 -0.002843621 -0.2990221 -0.4885461 [4,] -0.38740975 0.7800235 0.12819144 0.206188106 0.8481351 0.2572268 [5,] -0.07082702 -0.7870970 0.60560030 -1.381615740 1.4935228 0.1165892 [6,] -0.06916424 -0.5869168 0.39492984 0.016430970 -0.6531722 -0.1194990 > tapply(nummtx, markmtx, FUN=mean) a b c d e f g -0.168826070 -0.037543099 -0.334783145 0.173601720 0.527161589 0.257226798 -0.085579151 h i -0.131208561 -0.001454904 > matrix(tapply(nummtx, markmtx, FUN=mean), nrow=3) [,1] [,2] [,3] [1,] -0.1688261 0.1736017 -0.085579151 [2,] -0.0375431 0.5271616 -0.131208561 [3,] -0.3347831 0.2572268 -0.001454904 -- David Winsemius On Feb 16, 2009, at 12:43 PM, Titus von der Malsburg wrote:> > Hi list! I have a large matrix which I'd like to partition into > blocks > and for each block I'd like to compute the mean. Following a example > where each letter marks a block of the partition: > > a a a d g g > a a a d g g > a a a d g g > b b b e h h > b b b e h h > c c c f i i > > I'm only interested in the resulting matrix of means. How can this be > done efficiently? > > Thanks! Titus > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.