R: Compute multiplication efficiently

If I have a matrix A (5000-by-6000), which means it has n rows and m columns, it a_ij

is an element. I would like to calculate sum (k=1..m, l=1..m) (a[i,l]*a[j,k])

(ie this formula ) that is 5000 by 5000. What is the efficient way to calculate such a formula?

Thanks in advance.

+3


source to share


1 answer


You can simplify your expression so that the double sum of products becomes the product of two lines i

and j

. So your inference matrix is โ€‹โ€‹essentially a Kronecker product of rows:



x <- rowSums(a)
x %o% x

      

+4


source







All Articles