Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gram-Schmidt orthogonalization

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.

like image 674
George Dontas Avatar asked Dec 08 '22 02:12

George Dontas


1 Answers

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?

like image 184
Richard Herron Avatar answered Dec 09 '22 16:12

Richard Herron