Dear all, I have a simple question which I unfortunately do not seem to be able to solve myself. I have a (NxK) matrix and want to generate a new matrix by multiplying each row with itself such that the new matrix has dimension ((N*K)xK) (or better, generate an array with dimension (K,K,N)). I tried apply, but that did not work. Any suggestions? Thanks! Stephan ## Here is a simple example: u <- matrix(c(1,2,3,4,5,6,7,8,9,10),nrow=2) ## What I want to obtain u[1,]%*%t(u[1,]) u[2,]%*%t(u[2,]) ## stacked together --> 10x5 matrix ## This does not work sq <- function(x)x%*%t(x) apply(u,1,function(y)sq(y)) -- ----------------------- Stephan Lindner University of Michigan
Try this: matrix(apply(u, 1, tcrossprod), nr = nrow(u)*ncol(u), byrow = T) On Mon, Jan 19, 2009 at 3:39 PM, Stephan Lindner <lindners@umich.edu> wrote:> Dear all, > > > I have a simple question which I unfortunately do not seem to be able > to solve myself. I have a (NxK) matrix and want to generate a new > matrix by multiplying each row with itself such that the new matrix > has dimension ((N*K)xK) (or better, generate an array with dimension > (K,K,N)). I tried apply, but that did not work. Any suggestions? > > Thanks! > > > Stephan > > > > ## Here is a simple example: > > u <- matrix(c(1,2,3,4,5,6,7,8,9,10),nrow=2) > > ## What I want to obtain > > u[1,]%*%t(u[1,]) > u[2,]%*%t(u[2,]) > > ## stacked together --> 10x5 matrix > > > ## This does not work > > sq <- function(x)x%*%t(x) > apply(u,1,function(y)sq(y)) > > > > > > > -- > ----------------------- > Stephan Lindner > University of Michigan > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Dear Stephan, Try this: do.call(rbind,lapply(1:2,function(x) matrix(u[x,]%*%t(u[x,]),ncol=ncol(u)))) HTH, Jorge On Mon, Jan 19, 2009 at 12:39 PM, Stephan Lindner <lindners@umich.edu>wrote:> Dear all, > > > I have a simple question which I unfortunately do not seem to be able > to solve myself. I have a (NxK) matrix and want to generate a new > matrix by multiplying each row with itself such that the new matrix > has dimension ((N*K)xK) (or better, generate an array with dimension > (K,K,N)). I tried apply, but that did not work. Any suggestions? > > Thanks! > > > Stephan > > > > ## Here is a simple example: > > u <- matrix(c(1,2,3,4,5,6,7,8,9,10),nrow=2) > > ## What I want to obtain > > u[1,]%*%t(u[1,]) > u[2,]%*%t(u[2,]) > > ## stacked together --> 10x5 matrix > > > ## This does not work > > sq <- function(x)x%*%t(x) > apply(u,1,function(y)sq(y)) > > > > > > > -- > ----------------------- > Stephan Lindner > University of Michigan > > ______________________________________________ > 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]]