forked from andrewssobral/nway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsm.m
37 lines (32 loc) · 1.03 KB
/
gsm.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function [E]=GSM(V);
%GSM orthogonalization
%
% [E]=GSM(V);
% GS Gram-Schmidt Method for orthogonalisation
% An orthonormal basis spanning the columns of V is returned in E.
%
% This algorithm does not use pivoting or any other
% stabilization scheme. For a completely safe orthogonalization
% you should use 'ORTH()' though is may take triple the time.
% 'GSM()' is optimized for speed and requies only minimum storage
% during iterations. No check of rank is performed on V!
%
% Copyright (C) 1995-2006 Rasmus Bro & Claus Andersson
% Copenhagen University, DK-1958 Frederiksberg, Denmark, [email protected]
[m n]=size(V);
%Allocate space for the basis
E=zeros(m,n);
%The first basis vector is taken directly from V
s=sqrt(sum(V(:,1).^2));
E(:,1)=V(:,1)/s;
%Find the other basis vectors as orthogonals to
%the already determined basis by projection
for k=2:n,
f=V(:,k)-E(:,1:(k-1))*(E(:,1:(k-1))'*V(:,k));
s=sqrt(sum(f.^2));
if s<eps,
E(:,k)=0*f; %set to zeros
else
E(:,k)=f/s; %normalize
end;
end;