For the life of me I couldn't work out what to searc I have an m*n numeric matrix x and a numeric vector y (of length m+n-1) How do I do a calculation like this? z[i,j] = x[i,j] * y[i+j] ? Well, one can write a pair of loops, or write a single loop within which we calculate a vector at a time, but ... is there a "neat" way to do it? tiny example: x<-matrix(data=c(32,46,21,28,58,60,33,32,16,NA,10,15),nrow=4) y<-c(1.1,1.09,1.08,1.06,1.05,1.02) z<-matrix(nrow=4,ncol=3) z[,1]=x[,1]*y[1:4] z[,2]=x[,2]*y[2:5] z[,3]=x[,3]*y[3:6] which produces:> x[,1] [,2] [,3] [1,] 32 58 16 [2,] 46 60 NA [3,] 21 33 10 [4,] 28 32 15> y[1] 1.10 1.09 1.08 1.06 1.05 1.02> z[,1] [,2] [,3] [1,] 35.20 63.22 17.28 [2,] 50.14 64.80 NA [3,] 22.68 34.98 10.50 [4,] 29.68 33.60 15.30> z/x[,1] [,2] [,3] [1,] 1.10 1.09 1.08 [2,] 1.09 1.08 NA [3,] 1.08 1.06 1.05 [4,] 1.06 1.05 1.02 (this last to indicate what each element of x was multiplied by to produce z... well, apart from the NA) Thanks for any pointers Glen -- View this message in context: http://www.nabble.com/z-i%2Cj--%3D-x-i%2Cj--*-y%28i%2Bj%29---tp24731799p24731799.html Sent from the R help mailing list archive at Nabble.com.
Try this: z <- x * y[row(x) + col(x)] z2 <- x for(i in 1:nr) for(j in 1:nc) z2[i,j] <- x[i,j] * y[i+j] all.equal(z, z2) # TRUE On Wed, Jul 29, 2009 at 11:56 PM, glen_b<glnbrntt at gmail.com> wrote:> > > For the life of me I couldn't work out what to searc > > I have an m*n numeric matrix x and a numeric vector y (of length m+n-1) > > How do I do a calculation like this? > > z[i,j] = x[i,j] * y[i+j] ? > > > Well, one can write a pair of loops, or write a single loop > within which we calculate a vector at a time, but ... > > is there a "neat" way to do it? > > tiny example: > x<-matrix(data=c(32,46,21,28,58,60,33,32,16,NA,10,15),nrow=4) > y<-c(1.1,1.09,1.08,1.06,1.05,1.02) > z<-matrix(nrow=4,ncol=3) > > z[,1]=x[,1]*y[1:4] > z[,2]=x[,2]*y[2:5] > z[,3]=x[,3]*y[3:6] > > which produces: > >> x > ? ? [,1] [,2] [,3] > [1,] ? 32 ? 58 ? 16 > [2,] ? 46 ? 60 ? NA > [3,] ? 21 ? 33 ? 10 > [4,] ? 28 ? 32 ? 15 > >> y > [1] 1.10 1.09 1.08 1.06 1.05 1.02 > >> z > ? ? ?[,1] ?[,2] ?[,3] > [1,] 35.20 63.22 17.28 > [2,] 50.14 64.80 ? ?NA > [3,] 22.68 34.98 10.50 > [4,] 29.68 33.60 15.30 > >> z/x > ? ? [,1] [,2] [,3] > [1,] 1.10 1.09 1.08 > [2,] 1.09 1.08 ? NA > [3,] 1.08 1.06 1.05 > [4,] 1.06 1.05 1.02 > > (this last to indicate what each element of x was multiplied by to produce > z... well, apart from the NA) > > Thanks for any pointers > > Glen > > -- > View this message in context: http://www.nabble.com/z-i%2Cj--%3D-x-i%2Cj--*-y%28i%2Bj%29---tp24731799p24731799.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
I take it you mean z[i,j] = x[i,j]*y[i+j-1] (as you had it z[1,1] = x[1,1]*y[2], whereas your example suggests is should be y[1]) Here is a suggestion:> z <- x * y[outer(1:nrow(x), 1:ncol(x), "+")-1] > z[,1] [,2] [,3] [1,] 35.20 63.22 17.28 [2,] 50.14 64.80 NA [3,] 22.68 34.98 10.50 [4,] 29.68 33.60 15.30>Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of glen_b Sent: Thursday, 30 July 2009 1:57 PM To: r-help at r-project.org Subject: [R] z[i,j] = x[i,j] * y(i+j) ? For the life of me I couldn't work out what to searc I have an m*n numeric matrix x and a numeric vector y (of length m+n-1) How do I do a calculation like this? z[i,j] = x[i,j] * y[i+j] ? Well, one can write a pair of loops, or write a single loop within which we calculate a vector at a time, but ... is there a "neat" way to do it? tiny example: x<-matrix(data=c(32,46,21,28,58,60,33,32,16,NA,10,15),nrow=4) y<-c(1.1,1.09,1.08,1.06,1.05,1.02) z<-matrix(nrow=4,ncol=3) z[,1]=x[,1]*y[1:4] z[,2]=x[,2]*y[2:5] z[,3]=x[,3]*y[3:6] which produces:> x[,1] [,2] [,3] [1,] 32 58 16 [2,] 46 60 NA [3,] 21 33 10 [4,] 28 32 15> y[1] 1.10 1.09 1.08 1.06 1.05 1.02> z[,1] [,2] [,3] [1,] 35.20 63.22 17.28 [2,] 50.14 64.80 NA [3,] 22.68 34.98 10.50 [4,] 29.68 33.60 15.30> z/x[,1] [,2] [,3] [1,] 1.10 1.09 1.08 [2,] 1.09 1.08 NA [3,] 1.08 1.06 1.05 [4,] 1.06 1.05 1.02 (this last to indicate what each element of x was multiplied by to produce z... well, apart from the NA) Thanks for any pointers Glen -- View this message in context: http://www.nabble.com/z-i%2Cj--%3D-x-i%2Cj--*-y%28i%2Bj%29---tp24731799p24731799.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Reasonably Related Threads
- [LLVMdev] [llvm-commits] [PATCH] BasicBlock Autovectorization Pass
- [LLVMdev] [llvm-commits] [PATCH] BasicBlock Autovectorization Pass
- [LLVMdev] [llvm-commits] [PATCH] BasicBlock Autovectorization Pass
- [LLVMdev] [llvm-commits] [PATCH] BasicBlock Autovectorization Pass
- Extracting data by row