To: Vicky Landsman
Cc: R-Help
Subject: Re: [R] Summing up matrices in a list
On Wed, 2005-03-16 at 18:21 +0200, Vicky Landsman wrote:
Dear all,
I think that my question is very simple but I failed to solve it.
I have a list which elements are matrices like
try this:
matSums - function(lis, na.rm=FALSE){
if(!is.list(lis) || !all(sapply(lis, is.matrix))) stop('lis' must
be a list containing 2-dimensional arrays)
dims - sapply(lis, dim)
n - dims[1, 1]
p - dims[2, 1]
if(!all(n == dims[1, ]) || !all(p == dims[2, ])) stop(the
matrices
Dear Vicky,
Actually, this question was asked before (about a year ago, I think).
Looping turns out to be not so bad a solution; check out the following
example (from a short-course that I taught):
--- snip
# to loop or not to loop?
# Example: summing a list of matrices
On Wed, 2005-03-16 at 18:21 +0200, Vicky Landsman wrote:
Dear all,
I think that my question is very simple but I failed to solve it.
I have a list which elements are matrices like this:
mylist
[[1]]
[,1] [,2] [,3]
[1,]135
[2,]246
[[2]]
[,1] [,2]
Vicky Landsman wrote on 3/16/2005 10:21 AM:
Dear all,
I think that my question is very simple but I failed to solve it.
I have a list which elements are matrices like this:
mylist
[[1]]
[,1] [,2] [,3]
[1,]135
[2,]246
[[2]]
[,1] [,2] [,3]
[1,]79 11
mylist - list( matrix(1:6, nc=3), matrix(7:12, nc=3) )
do.call(+, mylist)
[,1] [,2] [,3]
[1,]8 12 16
[2,] 10 14 18
Regards, Adai
On Wed, 2005-03-16 at 18:21 +0200, Vicky Landsman wrote:
Dear all,
I think that my question is very simple but I failed to solve it.
I have a
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Marc Schwartz
Sent: Wednesday, March 16, 2005 11:30 AM
To: Vicky Landsman
Cc: R-Help
Subject: Re: [R] Summing up matrices in a list
On Wed, 2005-03-16 at 18:21 +0200, Vicky
On Wed, 16 Mar 2005, Adaikalavan Ramasamy wrote:
mylist - list( matrix(1:6, nc=3), matrix(7:12, nc=3) )
do.call(+, mylist)
[,1] [,2] [,3]
[1,]8 12 16
[2,] 10 14 18
Yes, but this works only when the list is of length 2, when
M-mylist[[1]]+mylist[[2]] seems preferable.
Vicky Landsman msvika at mscc.huji.ac.il writes:
:
: Dear all,
: I think that my question is very simple but I failed to solve it.
: I have a list which elements are matrices like this:
:
: mylist
: [[1]]
: [,1] [,2] [,3]
: [1,]135
: [2,]246
:
: [[2]]
:
-525-9140x23604
http://socserv.mcmaster.ca/jfox
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Marc Schwartz
Sent: Wednesday, March 16, 2005 11:30 AM
To: Vicky Landsman
Cc: R-Help
Subject: Re: [R] Summing
Ramasamy [EMAIL PROTECTED]
To: Vicky Landsman [EMAIL PROTECTED]
Cc: R-help r-help@stat.math.ethz.ch
Sent: Wednesday, March 16, 2005 5:32 PM
Subject: Re: [R] Summing up matrices in a list
mylist - list( matrix(1:6, nc=3), matrix(7:12, nc=3) )
do.call(+, mylist)
[,1] [,2] [,3]
[1,]8 12 16
[2
Here's a slight variation:
lst - list(matrix(1:4, 2, 2), matrix(5:8, 2, 2), matrix(9:12, 2, 2))
m - matrix(0, 2, 2)
m[] - rowSums(do.call(cbind, lapply(lst, c)))
m
[,1] [,2]
[1,] 15 21
[2,] 18 24
This can probably be simplified even more with the `abind' package...
Andy
From:
To: Adaikalavan Ramasamy
Cc: R-help
Subject: Re: [R] Summing up matrices in a list
On Wed, 16 Mar 2005, Adaikalavan Ramasamy wrote:
mylist - list( matrix(1:6, nc=3), matrix(7:12, nc=3) )
do.call(+, mylist)
[,1] [,2] [,3]
[1,]8 12 16
[2,] 10 14 18
Yes, but this works
13 matches
Mail list logo