[R] addition of matrix

Yinghai Deng yinghai.deng at bri.nrc.ca
Tue Feb 19 22:34:20 CET 2008


Hello Keith,

Based on what you have done, please try this:

M<-matrix(nrow=num.rows, ncol=num.x.col*num.y.col)
for( i in 1:num.rows){M[i,]<-rep(Y[i,], each=num.y.col)}
Z<-Z+M

not elegant, but works.

HTH

YH Deng



-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org]On Behalf Of Marlin Keith Cox
Sent: February 19, 2008 2:07 PM
To: r-help at r-project.org
Subject: [R] addition of matrix


Thank you ahead of time for help with this.


I have two matrices

X

  [,1]   [,2]  [,3]   [,4]  [,5]   [,6]
 [1,]  660 693.00 726.0 759.00 792.0 825.00
 [2,]  548 575.40 602.8 630.20 657.6 685.00
 [3,]  676 709.80 743.6 777.40 811.2 845.00
 [4,]  763 801.15 839.3 877.45 915.6 953.75
 [5,]  768 806.40 844.8 883.20 921.6 960.00
 [6,]  692 726.60 761.2 795.80 830.4 865.00
 [7,]  657 689.85 722.7 755.55 788.4 821.25
 [8,]  630 661.50 693.0 724.50 756.0 787.50
 [9,]  748 785.40 822.8 860.20 897.6 935.00
[10,]  680 714.00 748.0 782.00 816.0 850.00

and

Y

       [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
 [1,]  1369.00  1396.517  1424.308  1452.372  1480.710  1509.323
 [2,]  1521.00  1551.572  1582.448  1613.629  1645.114  1676.903
 [3,]  1521.00  1551.572  1582.448  1613.629  1645.114  1676.903
 [4,]  1521.00  1551.572  1582.448  1613.629  1645.114  1676.903
 [5,]  1600.00  1632.160  1664.640  1697.440  1730.560  1764.000
 [6,]  1722.25  1756.867  1791.829  1827.135  1862.786  1898.781
 [7,]  1936.00  1974.914  2014.214  2053.902  2093.978  2134.440
 [8,]  2025.00  2065.703  2106.810  2148.323  2190.240  2232.563
 [9,]  2116.00  2158.532  2201.486  2244.864  2288.666  2332.890
[10,]  2209.00  2253.401  2298.244  2343.528  2389.254  2435.423

Each row (1-10) is an individual and columns 1-6 are treatments to each
individual.  I need to look at interactions between the two variables X and
Y, by rows

example
1369 + (660*1369 ^ -1)
1396.517 + (660*1396.517 ^ -1)
1424.308 + (660*1424.308 ^ -1)

...
1369 + (693*1369 ^ -1
1396.517 + (693*1396.517 ^ -1
1424.308 + (693*1424.308 ^ -1
...
etc.

so each row would yield 36 numbers.

I have the following which works for X/Y, but I need Y+X/Y

num.x.col <- length(X[1,])
num.y.col <- length(Y[1,])
num.rows <- length(X[,1])

Z <- matrix(nrow=num.rows, ncol=num.x.col*num.y.col)

for( i in 1:num.rows)  {
       Z[i,] <- as.vector(X[i,] %*% t(Y[i,])^-1 )
}

Any help with making this be Y + X/Y would be appreciated.

--
Keith Cox, Ph.D.
Sitka Sound Science Center
Fisheries Biologist
P.O. Box 464
Sitka, Alaska, 99835

907 752-0563
marlinkcox at gmail.com

	[[alternative HTML version deleted]]

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



More information about the R-help mailing list