Skip to content
Prev 175987 / 398503 Next

matrix vectorization or something else??

On Apr 3, 2009, at 1:36 AM, Kutlwano Ramaboa wrote:

            
Not necessary to form the transpose of the row vectors because the %*%  
infix operator implicitly forms the transpose of the first argument.
For the first row, you can get all cross-products with:
apply(X, 1, function(x) x %*% X[1,] )
# [1]  55 130 205 280

Now form a matrix to hold the 4 sets:

xx <- matrix(NA, nrow=4,ncol=4)
for (i in 1:nrow(X) ) {xx[i, ] <- apply(X, 1, function(x) x %*% X[i,] )}

# Since you don't want the diagonal X[i, ]%*%X[i, ], zero them out:
  diag(xx) <- 0
 > xx
      [,1] [,2] [,3] [,4]
[1,]    0  130  205  280
[2,]  130    0  530  730
[3,]  205  530    0 1180
[4,]  280  730 1180    0

sum(Y[1:4]*xx[1:4,])
# [1] 35800

You still have not really specified what your expectation was, so  
there is no way to assess success at this end of the email-breathing- 
straw