How about ;
m <- matrix(rnorm(9), ncol = 3)
mm <- matrix(rnorm(9), ncol = 3)
sum(sapply(1:3, function(i, m, mm) m[i, ] %*% mm[ ,i], m, mm))
**
what is the best way to calculate the trace of a product
of two matrices?
If
A <- matrix(rnorm(48),nrow=6)
B <- matrix(r
It is still better to do
sum( A * t(B) )
Sorry!!
Carlos J. Gil Bellosta
http://www.datanalytics.com
Quoting Robin Hankin <[EMAIL PROTECTED]>:
> Hi
>
> what is the best way to calculate the trace of a product
> of two matrices?
>
> If
>
> A <- matrix(rnorm(48),nrow=6)
> B <- matrix(rnorm(48),nr
Try
sum( rowSums( A * t(B)) )
Then you do not make any calculation you do not really need.
Carlos J. Gil Bellosta
http://www.datanalytics.com
Quoting Robin Hankin <[EMAIL PROTECTED]>:
> Hi
>
> what is the best way to calculate the trace of a product
> of two matrices?
>
> If
>
> A <- matrix(rn
Hi
what is the best way to calculate the trace of a product
of two matrices?
If
A <- matrix(rnorm(48),nrow=6)
B <- matrix(rnorm(48),nrow=8)
Is there a better (faster, more elegant) way to compute the trace of
A%*%B than
sum(diag(A %*% B))?
I would call the above solution inelegant beca