[R] Row comparisons to a new matrix?

Atte Tenkanen attenka at utu.fi
Fri Oct 6 23:25:28 CEST 2006


I have to focus my question a little. I'd like to get rid of those for-loops and use, if possible, some faster method for creating the symmetric result matrix. 

Atte

> ?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 
> betweenall rows in a matrix (here A) and put the results to the new 
> symmetricmatrix? 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.htmland provide commented, minimal, self-contained, 
> reproducible code.
> 
>



More information about the R-help mailing list