www.pudn.com > GramSchmidt.zip > GramSchmidt.m


function [w,a]=GramSchmidt(p) 
    [row,column] = size(p); 
    w = zeros(row,column); 
    w(:,1) = p(:,1); 
    a = eye(column); 
    for k = 2:column 
        b = zeros(row,1); 
        for i = 1:k-1 
            temp = w(:,i)'*w(:,i); 
            if temp<10^-8 
                temp = 0.00001; 
            end 
            a(i,k) = w(:,i)'*p(:,k)/temp; 
            b = b+a(i,k)*w(:,i); 
        end 
        w(:,k) = p(:,k)-b; 
    end 
%%%%%%%%%%%%%%%%%%%%    normlization    %%%%%%%%%%%%     
    tempMatrixA = zeros(column,column); 
    tempMatrixB = zeros(column,column); 
    for i=1:column 
        tempNorm=norm(w(:,i)); 
        tempMatrixA(i,i) = 1/tempNorm; 
        tempMatrixB(i,i) = tempNorm; 
    end 
     
    w=w*tempMatrixA; 
    a=tempMatrixB*a; 
     
    clear tempMatrixA; 
    clear tempMatrixB;