1

I have a matrix of dimension n*k and I am trying to calculate another matrix which has all the pairs which will have n(n-1)/2 rows and k columns

New columns are element-wise multiplication of two different individual rows.

Below is the code,how i am calculating it currently:

mat = matrix(rnorm(10000,mean = 0,sd = 1), nrow = 100, ncol = 100) # n = 100, k = 100

mat2 = matrix(data = NA, nrow = nrow(mat)*(nrow(mat)-1)/2, ncol = ncol(mat))

counter = 0

for(i in seq(nrow(mat))){
    # print(paste('i', i))
    for(j in seq(nrow(mat))){
        # print(paste('j', j))
        if((i!= j)&(i<j)){
            
            x = mat[i,]*mat[j,]
            # print(x)
            
            mat2 = rbind(mat2, x)
            counter = counter + 1
            print(paste('Counter - ', counter,' : Pair(', i,',', j, ')', sep = ''))
        }
           
    }
}

Its very brute force approach though with O(n^2). Is there a way to do the same computation with less time ?

heisenbug47
  • 176
  • 1
  • 12

0 Answers0