[R] Row comparisons to a new matrix?

Atte Tenkanen attenka at utu.fi
Fri Oct 6 22:53:42 CEST 2006


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



More information about the R-help mailing list