[R] Matrix mulitplication

arun smartpink111 at yahoo.com
Wed Sep 11 19:29:22 CEST 2013


Hi,
Try:


set.seed(445)
A<- matrix(sample(1:20,124*5,replace=TRUE),ncol=5)

set.seed(42)
B<- matrix(sample(1:25,12*5,replace=TRUE),ncol=5)
 res<- sapply(seq_len(nrow(A)),function(i) colSums(A[i,]*t(B)))

 dim(res)
#[1]  12 124

A.K.



________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Wednesday, September 11, 2013 11:37 AM
Subject: 




Dear Arun,
Thanks for your previous reply. i have another question. if i have to matrix A and matrix B. matrix A is of dimension 124 row * 5 columns and B has a dimension of 12*5. 

What i want is to multiply each column of matrix B with each element of corresponding column A and then add.
more precisely

For Row 1 of A
A[1,1]*B[,1]+A[1,2]*B[,2]+A[1,3]*B[,3]+A[1,4]*B[,4]+A[1,5]*B[,5]


For Row 2 of A
A[2,1]*B[,1]+A[2,2]*B[,2]+A[2,3]*B[,3]+A[2,4]*B[,4]+A[2,5]*B[,5]

So in the end we should have a matrix of 12 rows and 124 columns.

I hope i m clear. if you feel any problem please tell me.

Eliza



More information about the R-help mailing list