views:

53

answers:

2

How do I normalize/scale matrices in R by column. For example, when I compute eigenvectors of a matrix, R returns:

> eigen(matrix(c(2,-2,-2,5),2,2))$vectors
       [,1]       [,2]
[1,] -0.4472136 -0.8944272
[2,]  0.8944272 -0.4472136

// should be normalized to
     [,1] [,2]
[1,]   -1   -2
[2,]    2   -1

The function "scale" subtracts the means and divided by standard deviation by column which does not help in this case. How do I achieve this?

A: 

This produces the matrix you say you want:

> a <- eigen(matrix(c(2,-2,-2,5),2,2))$vectors
> a / min(abs(a))
     [,1] [,2]
[1,]   -1   -2
[2,]    2   -1

But I'm not sure I understand exactly what you want, so this may not do the right thing in general.

Zack
A: 

Wolfram Alpha gives the following result:

http://www.wolframalpha.com/input/?i=eigenvalues{{2,-2},{-2,5}}

Input:

alt text

Eigenvalues:

alt text

Eigenvectors:

alt text

I'm not sure what you're talking about with means and standard deviations. A good iterative method like QR should get you the eigenvalues and eigenvectors you need. Check out Jacobi or Householder.

You normalize any vector by dividing every component by the square root of the sum of squares of its components. A unit vector will have magnitude equal to one.

In your case this is true: the vectors being presented by R have been normalized. If you normalize the two Wolfram eigenvectors, you'll see that both have a magnitude equal to the square root of 5. Divide each column vector by this value and you'll get the ones given to you by R. Both are correct.

duffymo
This isn't especially helpful, as the OP has already computed eigenvectors (that's what `eigen()` does) but they come out unnormalized (presumably because the underlying LAPACK routine doesn't bother to normalize them). Not that I understand exactly how the OP wants their eigenvectors normalized myself.
Zack
Divide all components by 0.4472136 and you'll get the values you want. I think it is helpful to have the names of algorithms pointed out, because you ought to know what's behind the eigen() method. It's also good to know that the description of what scale() is doing is incorrect. Eigenvector normalization is not done that way.
duffymo