s-news
[Top] [All Lists]

Responses: Element wise multiplication

To: s-news@lists.biostat.wustl.edu
Subject: Responses: Element wise multiplication
From: "Vumani Dlamini" <dvumani@hotmail.com>
Date: Tue, 27 Mar 2001 08:13:32 -0000
This is a summary of the responses I received for my "element-wise by row" multiplication. I did not know what to call it, thought of "kronecker by column of first matrix" and it also might have been a bit confusing.

Thanks for the solutions.
---------------------------------------------------------

Question:
I would like to perform an element wise multiplication by row of two matrices(A*B), A and B being matrices with equal number of rows, and the resulting matrix having the number of rows of A or B but the number of columns being a product of the number of columns of both matrices.

I have used a naive way of combining vectors of different multiplication, but needed a general appoach.

=======================================================
Responses:
Here is a simple way to multiply two matricies in the manner chosen.


A[,rep(1:ncol(A),each=ncol(B))]*B[,rep(1:ncol(B),times=ncol(A))]

Example:

A<-matrix(c(1,10,100,1000),nrow=2)
B<-matrix(c(1,2,3,4,5,6,),nrow=2)

A
    [,1] [,2]
[1,]    1  100
[2,]   10 1000
B
    [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6


C<-A[,rep(1:ncol(A),each=ncol(B))]*B[,rep(1:ncol(B),times=ncol(A))]

C

    [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    5  100  300  500
[2,]   20   40   60 2000 4000 6000

If you want row and column names then:

dimnames(A)<-list(c("row1","row2"),c("A1","A2"))
dimnames(B)<-list(c("row1","row2"),c("B1","B2","B3"))
A
    A1   A2
row1  1  100
row2 10 1000
B
    B1 B2 B3
row1  1  3  5
row2  2  4  6

dimnames(C)[[2]]<-paste(dimnames(A)[[2]][rep(1:ncol(A),each=ncol(B))],
dimnames(B)[[2]][rep(1:ncol(B),times=ncol(A))],sep="")
C
    A1B1 A1B2 A1B3 A2B1 A2B2 A2B3
row1    1    3    5  100  300  500
row2   20   40   60 2000 4000 6000

==========================================================
An amusing problem. I have a solution, but I think there must be better
ones. Please summarize any (correct) results you get to the list.

Let A and B be your matrices

m<-nrow(A)
nb<-ncol(B)
na<-ncol(A)


biga<-matrix(rep(t(A),nb), byrow=T, nc=na)
biga<-diag(as.vector(B))%*% biga
result<-matrix(numeric(),nr=m,nc=nb*na)
for(i in 0:(nb-1)) result[,i*na+(1:na)]<-biga[i*m+(1:m),]

==========================================================
I'm not sure exactly what you need, but the following may help.

f<-function(A,B){
      i<-row(array(NA, dim=c(ncol(A), ncol(B))))
      j<-col(array(NA, dim=c(ncol(A), ncol(B))))
      val <- A[,i]*B[,j]
      dimnames(val)[[2]]<-paste(i,j,sep=",") # not needed
      val
 }
A<-matrix(1:6,ncol=2)
B<-matrix(101:109,ncol=3)
A
    [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
B
    [,1] [,2] [,3]
[1,]  101  104  107
[2,]  102  105  108
[3,]  103  106  109
f(A,B)
    1,1 2,1 1,2 2,2 1,3 2,3
[1,] 101 404 104 416 107 428
[2,] 204 510 210 525 216 540
[3,] 309 618 318 636 327 654

The column labelled "i,j" is the product of the i'th column of A
and the j'th column of B.
==========================================================
This is not exactly what I think of as 'element-wise' multiplication.

You want the outer product of each of the corresponding rows of A and
B, i.e. A_{ij}*B_{ik}, all i,j,k, I think.

Off the top of my head:

index.rc <- matrix( 0, nr=ncol(A), nc=ncol(B) )

res <- A[, row(indx.rc) ]*B[, col(index.rc) ]

dim(res) <- c( nrow(A), length(indx.rc))

should do.
--------------------------------------------------------

Vumani Dlamini


_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


<Prev in Thread] Current Thread [Next in Thread>
  • Responses: Element wise multiplication, Vumani Dlamini <=