PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to calculate all the elements in X X^T in R?

 May 09, 2022     matrix, matrix-multiplication, product, r     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing