I tried this from the same website you were looking at, and then came across this question. I managed to get the script to work, but I get a different result.
#WORKING EXAMPLE #MAHALANOBIS DIST OF TWO MATRICES #define matrix mat1<-matrix(data=c(2,2,6,7,4,6,5,4,2,1,2,5,5,3,7,4,3,6,5,3),nrow=10) mat2<-matrix(data=c(6,7,8,5,5,5,4,7,6,4),nrow=5) #center data mat1.1<-scale(mat1,center=T,scale=F) mat2.1<-scale(mat2,center=T,scale=F) #cov matrix mat1.2<-cov(mat1.1,method="pearson") mat2.2<-cov(mat2.1,method="pearson") n1<-nrow(mat1) n2<-nrow(mat2) n3<-n1+n2 #pooled matrix mat3<-((n1/n3)*mat1.2) + ((n2/n3)*mat2.2) #inverse pooled matrix mat4<-solve(mat3) #mean diff mat5<-as.matrix((colMeans(mat1)-colMeans(mat2))) #multiply mat6<-t(mat5) %*% mat4 #multiply sqrt(mat6 %*% mat5)
I think that the function mahalanobis() used to calculate the distances of the mahalanobis between individuals (rows) in the same matrix. The pairwise.mahalanobis() function from package(HDMD) can compare two or more matrices and give mahalanobis the distance between the matrices.