BioChemoinformatics
BioChemoinformatics

Reputation: 715

R: Efficiently calculate multiplication'sum

If I have a matrix A (5000-by-6000), which means it has n rows and m columns, a_ij is the element. I would like to calculate sum (k=1..m, l=1..m) (a[i,l]*a[j,k]) (i.e., this formula), which is 5000-by-5000. What is an efficient way to calculate such a formula?

Thanks in advance.

Upvotes: 3

Views: 84

Answers (1)

flodel
flodel

Reputation: 89057

You can simplify your expression so the double sum of products becomes the product of the two rowsums i and j. So your output matrix is essentially the kronecker product of the rowsums:

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

Upvotes: 4

Related Questions