Hi, Not sure if this is what you wanted: mat1<- as.matrix(read.table(text=" 33 45 50 NA NA 54 ",sep="",header=FALSE)) mat2<- as.matrix(read.table(text=" 24 0.0000000 0.0000000 0.0000000 14 0.0000000 0.0000000 0.0000000 10 ",sep="",header=FALSE)) mat1%*%mat2 # V1 V2 V3 #[1,] 792 630 500 #[2,] NA NA NA
library(Matrix) mat1m<- as(mat1,"dgCMatrix") mat2m<- as(mat2,"dgCMatrix") tcrossprod(mat1m,mat2m) #2 x 3 sparse Matrix of class "dgCMatrix" # #[1,] 792 630 500 #[2,] NA NA 540 A.K. >Hi, > >I'm looking to multiply two matrices together, one of which may contain randomly placed NA values (i.e., there's no reason they will be all in a row or >column), but I still want an output like the example below: > >Matrix 1 >[1,] 33 45 50 >[2,] NA NA 54 > >Matrix 2 >[1,] A1 0.0000000 0.0000000 >[2,] 0.0000000 A2 0.0000000 >[3,] 0.0000000 0.0000000 A3 > > >Result >[1,] 33*A1 45*A2 50*A3 >[2,] NA NA (NA*0 +NA*0 +54*A3)=54*A3 > >Simply doing Matrix1%*%Matrix2 doesn't give what I want for the element in Row 2, Column 3 (it gives NA, which makes sense, but not sure how to >do what I'd like it to do). For my purposes, Matrix 2 will never have NA values, if that changes anything. ______________________________________________ R-help@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.