Gram Schmidt Process Algorithm

조회 수: 36 (최근 30일)
Ellenna K
Ellenna K 2015년 10월 5일
답변: Sebastián Soto 2022년 11월 9일
So I have an assignment to write algorithm code for Gram Schmidt process...I have done it in Matlab ,but when I run the code with input argument rand(some number), it gives me a result with variable ans... and some numbers in matrix... I want to create an output variable which won't be ans ,and can anyone see the code to tell me if it's okay? Thanks in advance,I would appreciate ur help..
% function Q = gramschmidt( A )
% FUNCTION computes orthonormal basis from independent vectors in A.
%
% Q = gramschmidt( A );
%
% INPUT :
% A - a matrix with *INDEPENDENT* vectors in columns
% (e.g. A = rand(3) will produce one)
%
% OUTPUT :
% Q - a matrix with orthonomal basis of A
%
%
% The vectors in A are independent BUT NOT YET orthonormal. Check A'*A. % If it is orthonormal, you should get strictly an identity matrix.
% number of vectors n = size( A, 2 );
% initialize output Q = zeros( n );
% turn every independent vector into a basis vector % (1) jth basis vector will be perpendicular to 1..j-1 previous found basis % (2) will be of length 1 (norm will be equal to 1) for j = 1 : n
% pick the jth independent vector
u = A( :, j );
% special case for j = 1: we will not run the following for loop. Will
% just normalize it and put as the first found basis. There are no
% previous basis to make orthogonal to.
% remove from raw "u" all components spanned on 1..j-1 bases, their
% contributions will be removed
% ==> this effectively makes jth independent vector orthogonal to all
% previous bases found in the previous steps.
% ==> enforcing orthogonality principle here. Not orthonormality yet.
for i = 1 : j - 1
u = u - proj( Q(:,i), A(:,j) );
end
% normalize it to length of 1 and store it
Q(:,j) = u ./ norm( u );
end
end
% projects a vector "a" on a direction "e" function p = proj( e, a )
% project "a" onto "e": (e' * a) / (e' * e) is the length (!) of "a" on "e" % multiplication by "e" is necessary to output the resulting vector which is % (colinear with "e") p = (e' * a) / (e' * e) .* e;
end
end

답변 (1개)

Sebastián Soto
Sebastián Soto 2022년 11월 9일
for i = 1 : j - 1
u = u - proj( Q(:,i), A(:,j) );
end
% normalize it to length of 1 and store it
Q(:,j) = u ./ norm( u );

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by