Hi, Can somebody tell me, which is the fastest way to make comparisons between all rows in a matrix (here A) and put the results to the new symmetric matrix? I have here used cosine distance as an example, but the comparison function can be any other, euclidean dist etc. A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) for(i in 1:length(A[,1])) { for(j in 1:length(A[,1])) { M[i,j]=cosine(A[i,],A[j,]) } } Atte Tenkanen University of Turku, Finland
?dist Bert Gunter Nonclinical Statistics 7-7374 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Atte Tenkanen Sent: Friday, October 06, 2006 1:54 PM To: r-help at stat.math.ethz.ch Subject: [R] Row comparisons to a new matrix? Hi, Can somebody tell me, which is the fastest way to make comparisons between all rows in a matrix (here A) and put the results to the new symmetric matrix? I have here used cosine distance as an example, but the comparison function can be any other, euclidean dist etc. A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) for(i in 1:length(A[,1])) { for(j in 1:length(A[,1])) { M[i,j]=cosine(A[i,],A[j,]) } } Atte Tenkanen University of Turku, Finland ______________________________________________ R-help at stat.math.ethz.ch 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.
There is a generalized inner product here: http://tolstoy.newcastle.edu.au/R/help/05/04/3709.html On 10/6/06, Atte Tenkanen <attenka at utu.fi> wrote:> Hi, > Can somebody tell me, which is the fastest way to make comparisons between all rows in a matrix (here A) and put the results to the new symmetric matrix? I have here used cosine distance as an example, but the comparison function can be any other, euclidean dist etc. > > A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) > > M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) > > for(i in 1:length(A[,1])) > { > for(j in 1:length(A[,1])) > { > M[i,j]=cosine(A[i,],A[j,]) > } > } > > Atte Tenkanen > University of Turku, Finland > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
An embedded message was scrubbed... From: Atte Tenkanen <attenka at utu.fi> Subject: Re: [R] Row comparisons to a new matrix? Date: Sat, 07 Oct 2006 20:34:37 +0300 Size: 2401 Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061007/f700181f/attachment.mht
Thanks Gabor, Your version is handy to use, because you can change the function as you like. However it isn't any faster and if you know some way to make the result matrix more quickly, I'm interested to learn it. My test material (musical improvisations) consists of samples with 20000x20000 or even bigger result matrices. Atte ----- Original Message ----- From: Gabor Grothendieck <ggrothendieck at gmail.com> Date: Saturday, October 7, 2006 0:28 am Subject: Re: [R] Row comparisons to a new matrix?> There is a generalized inner product here: > > http://tolstoy.newcastle.edu.au/R/help/05/04/3709.html > > On 10/6/06, Atte Tenkanen <attenka at utu.fi> wrote: > > Hi, > > Can somebody tell me, which is the fastest way to make > comparisons between all rows in a matrix (here A) and put the > results to the new symmetric matrix? I have here used cosine > distance as an example, but the comparison function can be any > other, euclidean dist etc. > > > > A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) > > > > M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) > > > > for(i in 1:length(A[,1])) > > { > > for(j in 1:length(A[,1])) > > { > > M[i,j]=cosine(A[i,],A[j,]) > > } > > } > > > > Atte Tenkanen > > University of Turku, Finland > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > >