# [R] centering matrix

Tue Jan 29 15:35:57 CET 2013

```Hello,

The question is a bit confusing.
If you nedd to compute B = X'X, all you have to do is

B <- t(X) %*% X

If you want to compute the other formula, the following avoids loops.

n <- nrow(d)
B <- -d^2/2 - rowSums(d^2)/n - colSums(d^2)/n + sum(d^2)/n^2

Hope this helps,

Em 28-01-2013 22:35, Eleonora Schiano escreveu:
> I have a matrix d representing distances.
> I need to find B=X'X
> X=matrix that generated these distances.+
>
> B[i,j]=-(1/2)*{[d[i,j]^2-[(1/n)*sum for i=1...n(d[i,j])^2]-[(1/n)sum for
> j=1...n(d[i,j])^2]+[(1/n^2)sum for i=1...n sum for j=1...n (d[i,j])^2]}
>
> it's right if i do
> B=matrix(,nrow=dim(d),ncol=dim(d))
> for (i in 1:dim(d))
> for (j in 1:dim(d)){
> B=(-(1/2))*(d[i,j]^2-d[i,]^2-d[,j]^2+d[,]^2)
> }
> or i have to put
> B[i,j]=(-(1/2))*(d[i,j]^2-d[i,]^2-d[,j]^2+d[,]^2)
> ??
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help