Issue
Let X be a matrix with n rows and p columns. I want to calculate all the p times p matrices obtained by multiplying the rows of X times the transposed of the same rows. This is, the first matrix is
X[1,]%*%t(X[1,])
I want to avoid a for loop, so I wonder how to obtain the n (p times p) matrices associated to
X[i,]%*%t(X[i,])
with a more direct command such as apply
or sweep
. Sorry, I just cannot figure out how to do it.
Solution
You should use crossprod
or tcrossprod
for operations like that (see here). In your case,
lapply(1:nrow(X), function(i) tcrossprod(X[i,]))
Answered By - linog Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.