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@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.

Reply via email to