Gram Schmidt Process Algorithm

41 Ansichten (letzte 30 Tage)
Ellenna K
Ellenna K am 5 Okt. 2015
Beantwortet: Sebastián Soto am 9 Nov. 2022
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

Antworten (1)

Sebastián Soto
Sebastián Soto am 9 Nov. 2022
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 );

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by