Hi, You may try: set.seed(49) m1 = matrix(rnorm(30), nrow = 3) m2 = matrix(rnorm(30), nrow = 3) corsP<-vector() for(i in 1:3) corsP[i] = cor(m1[i,], m2[i,]) corsP #[1] 0.58411274 -0.02382329 0.03760757
diag(cor(t(m1),t(m2))) #[1] 0.58411274 -0.02382329 0.03760757 #or mNew<- rbind(m1,m2) indx<-rep(seq(nrow(mNew)/2),2) sapply(split(seq_len(nrow(mNew)),indx),function(x) cor(t(mNew[x,]),t(mNew[x,]))[2]) # 1 2 3 #0.58411274 -0.02382329 0.03760757 #or tapply(seq_along(indx),list(indx),FUN=function(x) cor(t(mNew[x,]),t(mNew[x,]))[2]) # 1 2 3 #0.58411274 -0.02382329 0.03760757 A.K. ________________________________ From: Ira Sharenow <irasharenow...@yahoo.com> To: arun <smartpink...@yahoo.com> Sent: Sunday, September 22, 2013 9:57 PM Subject: Correlate rows of 2 matrices Arun, I have a new problem for you. I have two data frames (or matrices) and row by row I want to take the correlations. So if I have a 3 row by 10 column matrix, I would produce 3 correlations. Is there a way to merge the matrices and then use some sort of split? Ideas/solutions much appreciated. m1 = matrix(rnorm(30), nrow = 3) m2 = matrix(rnorm(30), nrow = 3) > set.seed(22) > m1 = matrix(rnorm(30), nrow = 3) > m2 = matrix(rnorm(30), nrow = 3) > for(i in 1:3) corsP[i] = cor(m1[i,], m2[i,]) > corsP [1] -0.50865019 -0.27760046 0.01423144 Thanks. Ira ______________________________________________ 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.