views:

63

answers:

2

Given a matrix A (not neccessarily square) with independent columns, I was able to apply Gram-Schmidt iteration and produce an orthonormal basis for its columnspace (in the form of an orthogonal matrix Q) using Matlab's function qr

A=[1,1;1,0;1,2]

[Q,R] = qr(A)

and then

>> Q(:,1:size(A,2))
ans =
  -0.577350269189626  -0.000000000000000
  -0.577350269189626  -0.707106781186547
  -0.577350269189626   0.707106781186547

You can verify that the columns are orthonormal

Q(:,1)'*Q(:,2) equals zero and

norm(Q(:,1)) equals norm(Q(:,2)) equals 1

Given a matrix that has independent columns (like A), is there a function in R that produces the (Gram-Schmidt) orthogonal matrix Q ?. R's qr function doesn't produce an orthogonal Q.

+1  A: 

A quick search via rseek.org leads to package far and its function orthonormalization which you could try.

Dirk Eddelbuettel
+4  A: 

qr works, but it uses a unique convention and produces a qr object that you further operate on with qr.Q and qr.R:

> A
     [,1] [,2]
[1,]    1    1
[2,]    1    0
[3,]    1    2
> A.qr <- qr(A)
> qr.Q(A.qr)
           [,1]          [,2]
[1,] -0.5773503 -5.551115e-17
[2,] -0.5773503 -7.071068e-01
[3,] -0.5773503  7.071068e-01
> qr.R(A.qr)
          [,1]      [,2]
[1,] -1.732051 -1.732051
[2,]  0.000000  1.414214

Is this the output you wanted?

richardh
Thanks, I just found it out reading the help file more carefully. I rushed to ask for help.
gd047