[R] Using apply to generate matrix from rows?
lindners at umich.edu
Mon Jan 19 18:39:31 CET 2009
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?
## 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
## stacked together --> 10x5 matrix
## This does not work
sq <- function(x)x%*%t(x)
University of Michigan
More information about the R-help