David Romano
2013-Feb-06 05:50 UTC
[R] how to "multiply" list of matrices by list of vectors
Hi everyone, I'd like to be able to apply lda to each 2D matrix slice of a 3D array, and then use the scalings to obtain the corresponding lda scores. I can use 'apply' to get a list of the lda output for each 2D slice, and can create a list of the resulting scalings, but I'm not sure how to multiply them in a vectorized way. Here's how I made a list of 2D matrices (suggestion on improving this would be welcome, too!):> aa <- array(1:24,c(4,2,3)) > mlist <- apply(aa,2,list) > mlist <- lapply(mlist, unlist) > mlist <- lapply(mlist, function(x) matrix(x,4,2))and here's how I made a list of vectors:> mm <- matrix(1:6,2,3) > vlist <- apply(mm, list) > vlist <- lapply(vlist, unlist)Now I'd like to make the list whose i-th element is mlist[[i]]%*%vlist[[i]] without having to loop through the indices. Any help would be appreciated! Thanks, David [[alternative HTML version deleted]]
Rolf Turner
2013-Feb-06 07:04 UTC
[R] how to "multiply" list of matrices by list of vectors
Try:
n <- length(mlist)
result <- lapply(1:n,function(i,m,v){m[[i]]%*%v[[i]]},m=mlist,v=vlist)
These days the use of lapply is unlikely to be much, if at all, faster than
the use of a for loop.
cheers,
Rolf Turner
On 02/06/2013 06:50 PM, David Romano wrote:> Hi everyone,
>
> I'd like to be able to apply lda to each 2D matrix slice of a 3D array,
and
> then use the scalings to obtain the corresponding lda scores.
>
> I can use 'apply' to get a list of the lda output for each 2D
slice, and
> can create a list of the resulting scalings, but I'm not sure how to
> multiply them in a vectorized way.
>
>
> Here's how I made a list of 2D matrices (suggestion on improving this
would
> be welcome, too!):
>
>> aa <- array(1:24,c(4,2,3))
>> mlist <- apply(aa,2,list)
>> mlist <- lapply(mlist, unlist)
>> mlist <- lapply(mlist, function(x) matrix(x,4,2))
> and here's how I made a list of vectors:
>
>> mm <- matrix(1:6,2,3)
>> vlist <- apply(mm, list)
>> vlist <- lapply(vlist, unlist)
> Now I'd like to make the list whose i-th element is
mlist[[i]]%*%vlist[[i]]
> without having to loop through the indices.
>
> Any help would be appreciated!
Hi,
I got an error message with:
vlist <- apply(mm, list)
Error in match.fun(FUN) : argument "FUN" is missing, with no default
#assuming that
vlist <- apply(mm,2,list)
mapply("%*%",mlist,vlist[1:2],SIMPLIFY=FALSE)
#[[1]]
?# ? ?[,1]
#[1,] ? 19
#[2,] ? 22
#[3,] ? 25
#[4,] ? 28
#
#[[2]]
?# ? ?[,1]
#[1,] ? 67
#[2,] ? 74
#[3,] ? 81
#[4,] ? 88
A.K.
----- Original Message -----
From: David Romano <dromano at stanford.edu>
To: r-help at r-project.org
Cc:
Sent: Wednesday, February 6, 2013 12:50 AM
Subject: [R] how to "multiply" list of matrices by list of vectors
Hi everyone,
I'd like to be able to apply lda to each 2D matrix slice of a 3D array, and
then use the scalings to obtain the corresponding lda scores.
I can use 'apply' to get a list of the lda output for each 2D slice, and
can create a list of the resulting scalings, but I'm not sure how to
multiply them in a vectorized way.
Here's how I made a list of 2D matrices (suggestion on improving this would
be welcome, too!):
> aa <- array(1:24,c(4,2,3))
> mlist <- apply(aa,2,list)
> mlist <- lapply(mlist, unlist)
> mlist <- lapply(mlist, function(x) matrix(x,4,2))
and here's how I made a list of vectors:
> mm <- matrix(1:6,2,3)
> vlist <- apply(mm, list)
> vlist <- lapply(vlist, unlist)
Now I'd like to make the list whose i-th element is mlist[[i]]%*%vlist[[i]]
without having to loop through the indices.
Any help would be appreciated!
Thanks,
David
??? [[alternative HTML version deleted]]
______________________________________________
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.
David Romano
2013-Feb-06 16:10 UTC
[R] how to "multiply" list of matrices by list of vectors
Thanks Rolf and Arun! -David On Wed, Feb 6, 2013 at 6:13 AM, arun <smartpink111@yahoo.com> wrote:> Hi, > > I got an error message with: > vlist <- apply(mm, list) > > Error in match.fun(FUN) : argument "FUN" is missing, with no default > #assuming that > vlist <- apply(mm,2,list) > > mapply("%*%",mlist,vlist[1:2],SIMPLIFY=FALSE) > #[[1]] > # [,1] > #[1,] 19 > #[2,] 22 > #[3,] 25 > #[4,] 28 > # > #[[2]] > # [,1] > #[1,] 67 > #[2,] 74 > #[3,] 81 > #[4,] 88 > > A.K. > ----- Original Message ----- > From: David Romano <dromano@stanford.edu> > To: r-help@r-project.org > Cc: > Sent: Wednesday, February 6, 2013 12:50 AM > Subject: [R] how to "multiply" list of matrices by list of vectors > > Hi everyone, > > I'd like to be able to apply lda to each 2D matrix slice of a 3D array, and > then use the scalings to obtain the corresponding lda scores. > > I can use 'apply' to get a list of the lda output for each 2D slice, and > can create a list of the resulting scalings, but I'm not sure how to > multiply them in a vectorized way. > > > Here's how I made a list of 2D matrices (suggestion on improving this would > be welcome, too!): > > > aa <- array(1:24,c(4,2,3)) > > mlist <- apply(aa,2,list) > > mlist <- lapply(mlist, unlist) > > mlist <- lapply(mlist, function(x) matrix(x,4,2)) > > and here's how I made a list of vectors: > > > mm <- matrix(1:6,2,3) > > vlist <- apply(mm, list) > > vlist <- lapply(vlist, unlist) > > Now I'd like to make the list whose i-th element is mlist[[i]]%*%vlist[[i]] > without having to loop through the indices. > > Any help would be appreciated! > > Thanks, > David > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
Maybe Matching Threads
- different behavior of $ with string literal vs string variable as argument
- odd behavior of browser()
- how to extract test for collinearity and constantcy used in lda
- using ifelse to remove NA's from specific columns of a data frame containing strings and numbers
- Get subset of n dimensional matrix