[R] centering matrix

Rui Barradas ruipbarradas at sapo.pt
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,

Rui Barradas

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)[1],ncol=dim(d)[2])
> for (i in 1:dim(d)[1])
> for (j in 1:dim(d)[2]){
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list