Gram matrix without loops

I need to find the Gram G matrix from the column vectors of the H matrix, for example. H = [h_1, h_2, ..., h_m] and G [i, j] = (h_i, h_j) for each i, j from 1 to m, where (,) denotes a dot product. I wrote the following code in R:

Gram <- function(H)
{
  m <- dim(H)[2]

  G <- matrix(NA, m, m)
  for(i in 1:m)
    for(j in 1:m)
      G[i, j] <- H[, i] %*% H[, j]

  G
}

      

Is there a way to avoid loops?

+3


source to share





All Articles