Skip to content

matrix vectorization or something else??

4 messages · Kutlwano Ramaboa, David Winsemius

#
Generally unambiguous questions get answered quickly. Perhaps other  
readers are having the same difficulties I am experiencing. So let's  
try to parse what you are asking:
On Apr 2, 2009, at 8:26 AM, Kutlwano Ramaboa wrote:

            
"sum the product of each row"  ==?  sum(   prod(x[i, ] * ?
"remaining j rows"  ==? What is "j", Is it nrow(X)-1 ? An what sort of  
"multiplication by rows" do you mean? The inner product?
"multiplied by j y values" ==? You defined Y but not y. Assuming that  
you meant Y values, then we are still left to speculate about which of  
the 5 Y values are suppose to be matched to the 3 rows of X.
Not to me.
?citation
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
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