Skip to content

sum

10 messages · Jerome Asselin, Barry Rowlingson, Sundar Dorai-Raj +6 more

#
Dear helpers

I have a list where each element is a matrix (the list is 
obtained with lapply). I want to sum those matrices. Is there a 
function to do that? The sum function sums all the elements...
--


http://adsl.sapo.pt
#
The only way I can see is to vectorize your list of matrices. Here's an 
example.

matlist <- lapply(1:10,function(i) matrix(rnorm(12),3,4))
summat <- matrix(sapply(matlist,I)%*%rep(1,10),3,4)

You can use the loop below to verify that the above solution is correct.

forsum <- matrix(0,3,4)
for(i in 1:10)
forsum <- forsum+matlist[[i]]

"summat" and "forsum" should be the same.

HTH,
Jerome
On April 23, 2003 10:57 am, Luis Silva wrote:
#
Luis Silva wrote:
Since your matrix elements must be all the same shape (rows x columns) 
for you to sum them, you could store them in an array (see ?array) 
rather than a list. Then all you need do is apply 'sum' to two 
dimensions of the array.

  Here's a one-liner that converts your list into an array (by unlisting 
it and then packing into an array with the right three dimensions) and 
then runs apply(...,c(1,2),sum) to get the answer you want:

  First some sample data:

 > foo
[[1]]
      [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[2]]
           [,1]      [,2]      [,3]
[1,] 0.1666667 0.5000000 0.8333333
[2,] 0.3333333 0.6666667 1.0000000

[[3]]
      [,1] [,2] [,3]
[1,]  0.5  0.5  0.5
[2,]  0.5  0.5  0.5

[[4]]
      [,1] [,2] [,3]
[1,] 0.01 0.01 0.01
[2,] 0.01 0.01 0.01

 > 
apply(array(unlist(foo),c(dim(foo[[1]])[1],dim(foo[[1]])[2],length(foo))),c(1,2),sum)
          [,1]     [,2]     [,3]
[1,] 1.676667 4.010000 6.343333
[2,] 2.843333 5.176667 7.510000

I'm sure there's a better way.

Baz
#
Luis Silva wrote:
How about:

R> r = list(matrix(1:4,2,2),matrix(5:8,2,2),matrix(9:12,2,2))
R> expr = paste("r[[",seq(along = r),"]]", collapse="+")
R> eval(parse(text = expr))
      [,1] [,2]
[1,]   15   21
[2,]   18   24


Regards,
Sundar
#
Can you create a 3-d array rather than a list?  If yes, "apply" will work.

hth.  spencer graves
Jerome Asselin wrote:
#
How about something like:
[,1] [,2] [,3] [,4]
[1,]    3    7   11   15
[2,]    4    8   12   16
[3,]    5    9   13   17
[4,]    6   10   14   18
-roger
_______________________________
UCLA Department of Statistics
http://www.stat.ucla.edu/~rpeng
On Wed, 23 Apr 2003, Luis Silva wrote:

            
#
On Wednesday 23 April 2003 12:57 pm, Luis Silva wrote:
If the list is not too long, perhaps

sumlist <- 
    function(x) 
        if (length(x) > 2) sumlist(x[1:2]) + 
             sumlist(x[-(1:2)]) else do.call("+", x) 



sumlist(<whatever your list is>)


-Deepayan
#
Dear Luis,
On Wednesday 23 April 2003 12:57 pm, Luis Silva wrote:
> I have a list where each element is a matrix (the list is
 > obtained with lapply). I want to sum those matrices. Is there a
 > function to do that? The sum function sums all the elements...

Here's a recursive function that works with a list of two or more matrices:

     sumMatrices <- function(matrices){
         if (length(matrices) > 2) matrices[[1]] + Recall(matrices[-1])
         else matrices[[1]] + matrices[[2]]
         }

Regards,
  John



-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
#
Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> writes:
Didn't someone do an abind() function at some point? (Generalizing
cbind/rbind)
l <- foo
apply(array(unlist(l),c(dim(l[[1]]),length(l))),c(1,2),sum)

is a bit shorter.

Even shorter, although I'm not sure better in all senses of the word,
same basic idea:

matrix(apply(sapply(l,c),1,sum),dim(l[[1]]))

However, aren't we ignoring the obvious?:

s<-0;(for(a in l)s<-s+a)