[R] matrix vectorization or something else??

David Winsemius dwinsemius at comcast.net
Fri Apr 3 14:26:04 CEST 2009




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

> Agree, it is ambiguous. Will try again....
>
> Suppose I have the following
>
> X<-matrix(c(1:20),nrow=4,byrow=T), hence this is a 4 by 5 matrix
> Y<-matrix(c(2,4,6,8),ncol=1), and this is a 4 by 1 vector, each  
> value denote y1, y2,y3, y4
>
> Step 1 - write each row of X as a vector, so I have vectors t(x_1),  
> t(x_2), t(x_3) and t(x_4)

Not necessary to form the transpose of the row vectors because the %*%  
infix operator implicitly forms the transpose of the first argument.
>
> Step 2 (inner product) ---- [prod(t(x_1) *x_2) + prod(t(x_1) *x_3) +  
> prod(t(x_1) *x_4) ] *y1

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

-- 
David Winsemius
>
> Repeat for each row of X, that is
>
>    [prod(t(x_2) *x_1) + prod(t(x_2) *x_3) + prod(t(x_2) *x_4)] *y2
>    [prod(t(x_3) *x_1) + prod(t(x_3) *x_2) + prod(t(x_3) *x_4)] *y3
>    [prod(t(x_4) *x_1) + prod(t(x_4) *x_2) + prod(t(x_4) *x_3)] *y4
>
>
> Hope this makes better sense ...
>
>
>
>
>
>
> > by the remaining j rows
>
> "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 (i.e sum( t(x_i) x_j y_j) )
>
> "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.
>
>
> >>> David Winsemius <dwinsemius at comcast.net> 2009/04/03 04:24 AM >>>
> 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:
>
> > Hello
> >
> > This may have been answered elsewhere, and I have looked on the web,
> > but nothing helps. I am trying to do the following:
> >
> > X<-matrix(c(1:15),nrow=3,byrow=T)
> > Y<-matrix(c(2,4,6,8,10),ncol=1)
> >
> > I need to sum the product of each row of X
>
> "sum the product of each row"  ==?  sum(   prod(x[i, ] * ?
>
> > by the remaining j rows
>
> "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 (i.e sum( t(x_i) x_j y_j) )
>
> "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.
>
> >
> >
> > Hope this makes sense.
>
> Not to me.
>
> >
> >
> > Thanks in advance.
> >
> > Ps: how do I reference all the help that I have had from the R:  
> team?
>
> ?citation
>
> >
> >
> > [[alternative HTML version deleted]]
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list