Projection using Modified Gram-Schmidt orthogonality
9 次查看(过去 30 天)
显示 更早的评论
M
2023-1-25
Hello,
I need the Modified Gram-Schmidt orthogonalization method in my Research.
I wrote the following code for the projection using the Classic Gram-Schmidt:
function[Xp] = Project(A,B)
Xp = [] ;
u1 = B;
for i = 1:1:6
u2 = A(i,:)- (A(i,:)*u1)/(u1'*u1) * u1';
Xp = [Xp;u2] ;
end
end
I faced problems to convert the Modified Gram-Schmidt orthogonalization method into MATLAB code, which is illustrated in the following link https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process
under section Numerical stability.
Can anyone help me in this problem please?
22 个评论
Torsten
2023-1-25
The question is what you want.
The two codes do very different things:
The first code orthonormalizes A, the second projects A on the subspace spanned by B.
M
2023-1-25
@Torsten see in the following link :
"Methods for performing orthogonalization include:
- Gram–Schmidt process, which uses projection"
M
2023-1-25
I wrote this code based on the classical Gram-Schmidt
function[Xp] = Project(A,B)
Xp = [] ;
u1 = B;
for i = 1:1:6
u2 = A(i,:)- (A(i,:)*u1)/(u1'*u1) * u1';
Xp = [Xp;u2] ;
end
end
Torsten
2023-1-25
编辑:Torsten
2023-1-25
As said, projecting a set of vectors (rows of A) on one other vector (B) doesn't produce any problems.
Maybe the real problem you are trying to solve is different from what you do, but my abilities as clairvoyant are limited. So you should describe what you are trying to do. If it is really projecting a set of vectors on one given vector, you don't need Gram-Schmid - you only need the projection formula supplied. If you want to project a vector on the span of a set of vectors, the problem will be different.
M
2023-1-25
编辑:M
2023-1-25
@Torsten Ok, in my research there is a matrix that should be orthogonalized with a certain vector ( I want to use this info in the cost function). I searched on google about the vector orthogonalization technique and I found in the following link https://en.wikipedia.org/wiki/Orthogonalization
"Methods for performing orthogonalization include:
Gram–Schmidt process, which uses projection"
And now i am trying to apply this method, it uses projection to performe orthogonalization, I tried the classic method and it gives a resonable answers but as i said i want to try the modified one , but i need a help in it!!
Torsten
2023-1-25
编辑:Torsten
2023-1-25
The statement "there is a matrix that should be orthogonalized with a certain vector" makes no sense to me. Could you elaborate ?
If it means that you want to extract the portion of each row of A that is orthogonal to this certain vector, then your function "Project" does the job. But since one vector is compared with only one other vector, there is no such thing as "modified Gram Schmid" for this application.
M
2023-1-25
编辑:M
2023-1-25
@Torsten but if you check the section
Numerical stability
in the following link : https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process
you will find something different from what you said!
Torsten
2023-1-25
编辑:Torsten
2023-1-25
I already wrote that I don't understand what you mean by
In my problem there is a matrix A is always orthogonalized with vector B.
As a consequence, I also don't understand what you mean by
I want to take advantage from this info and apply orthognalization methods as a cost function
Maybe someone else in the forum gets it. Or you make an attempt to explain better.
M
2023-1-25
@Torsten I just want to convert the section Numerical stability in the following link which illustrates the modified Gram-Schmidt : https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process
to matlab code in any example , because it faild with me.
This is all what I want, And then I will apply it to my variables.
Torsten
2023-1-25
You don't have a Gram-Schmid orthogonalization if you project several vectors separately on only one other vector. And this is what your "Project" function does. So there is no such thing as "Numerical Stability" you have to care about or that would change the projection formula.
If you want to project B on the row space of A, this would be a different thing.
But I repeat myself ...
Matt J
2023-1-25
Torsten
2023-1-25
in this case is there a need to use the Gram-Schmid method?
Yes. The easiest way is to orthonormalize the row space first (maybe using modified Gram-Schmid) and then use the formula under
Torsten
2023-1-26
The easiest way is to orthonormalize the row space first (maybe using modified Gram-Schmid) and then use the formula under
Anything you didn't understand in my answer ? Or wasn't it what you were asking for ?
Torsten
2023-1-26
Then I can assure you that from what you wrote, nobody will be able to understand what you are looking for.
Try to understand the problem first before looking for a solution.
采纳的回答
Matt J
2023-1-25
编辑:Matt J
2023-1-26
Aorth=orth(A); %A orthogonalized
ProjB=Aorth*(Aorth.'*B); %projection of B
38 个评论
Matt J
2023-1-26
But you have been asked why you need Modified Gram-Schmidt specifically and haven't told us. What does it give you that orth() does not?
M
2023-1-26
编辑:M
2023-1-26
@Matt J Because I am working in an analytical problem and I need to discuss the difference of using several orthogonal techniques regarding my problem in the research but I have a problem in coding the Modified Gram-Schmidt and this is what i asked about in the question . Thanks
Torsten
2023-1-26
but I have a problem in coding the Modified Gram-Schmidt and this is what i asked about in the question .
The modified Gram-Schmidt method was perfectly coded in the program you deleted from your question.
M
2023-1-26
编辑:M
2023-1-26
@Torsten I need the the projection algorithim of the Modified Gram-Schmidt , same as what I implemmented in the Classic Gram-Schmidt, Same as what illustrated in the section
Numerical stability
in the following link : https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process
M
2023-1-26
This is the code that i have delete it
function [Q,R] = mgs(X)
% Modified Gram-Schmidt. [Q,R] = mgs(X);
% G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998.
[n,p] = size(X);
Q = zeros(n,p);
R = zeros(p,p);
for k = 1:p
Q(:,k) = X(:,k);
for i = 1:k-1
R(i,k) = Q(:,i)'*Q(:,k);
Q(:,k) = Q(:,k) - R(i,k)*Q(:,i);
end
R(k,k) = norm(Q(:,k))';
Q(:,k) = Q(:,k)/R(k,k);
end
end
Torsten
2023-1-26
how to use the Q to obtian the Projection of A on B ?
ok, we go round in circles. The projection of vector A_j on B is
A_proj(j,:) = (A(j,:)*B)/(B.'*B) * B;
and a modified Gram-Schmidt method is not necessary to compute this projection.
Matt J
2023-1-26
编辑:Matt J
2023-1-26
Q contains the orthonormal basis from the modified Gram Schmidt procedure...So don't delete codes that solve your problem.
Except that it would probably be more efficient to just use Matlab's qr command if they give the same result,
A=rand(5,3);
Qmgs=mgs(A)
Qmgs = 5×3
0.0841 0.5572 -0.1058
0.5426 0.1513 -0.6618
0.7485 -0.2152 0.5957
0.0052 0.7840 0.3870
0.3718 0.0754 -0.2149
[Q,R]=qr(A,0);
Q=Q.*sign(diag(R)')
Q = 5×3
0.0841 0.5572 -0.1058
0.5426 0.1513 -0.6618
0.7485 -0.2152 0.5957
0.0052 0.7840 0.3870
0.3718 0.0754 -0.2149
function [Q,R] = mgs(X)
% Modified Gram-Schmidt. [Q,R] = mgs(X);
% G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998.
[n,p] = size(X);
Q = zeros(n,p);
R = zeros(p,p);
for k = 1:p
Q(:,k) = X(:,k);
for i = 1:k-1
R(i,k) = Q(:,i)'*Q(:,k);
Q(:,k) = Q(:,k) - R(i,k)*Q(:,i);
end
R(k,k) = norm(Q(:,k))';
Q(:,k) = Q(:,k)/R(k,k);
end
end
M
2023-1-26
@Torsten also I am repeating myself,my goal is to guarantee the orthogonality between A and B. Not THE PROJECTION itself! Gram Schmidt uses the projection as a tool to get the orthogonality .
For example House Householder transformation uses reflection to perform the orthogonalization and so on ....
M
2023-1-26
编辑:M
2023-1-26
"Methods for performing orthogonalization include:
- Gram–Schmidt process, which uses projection"
I need to study its efficiency to solve my problem. I dont need just final answer!
Torsten
2023-1-26
You need Gram-Schmidt if you want to compute a projection on a set of vectors.
So if you want to project B on A, you need Gram-Schmidt.
If you want to project A on B, you need one single line of code.
M
2023-1-26
编辑:M
2023-1-26
@Torsten so good , I need this part "( A(j,:) - (A(j,:)*B)/(B.'*B) * B )." in my problem. This is what we call classic Gram–Schmidt process. but there is a simple modification in the modified Gram–Schmidt it should give the same answer of this part "( A(j,:) - (A(j,:)*B)/(B.'*B) * B )." .. But the coding of it faild with me.
M
2023-1-26
@Torsten Ok , suppose i want to project B on A using the modified Gram–Schmidt , How can I code that?
Torsten
2023-1-26
编辑:Torsten
2023-1-26
( A(j,:) - (A(j,:)*B)/(B.'*B) * B )." in my problem. This is what we call classic Gram–Schmidt process
This is what we call "projecting a set of vectors A onto a single vector B". There is no iterative process as in the Gram-Schmidt procedure involved. And since there is no Gram-Schmidt, there is no Modified Gram-Schmidt, either.
Matt J
2023-1-26
编辑:Matt J
2023-1-26
The projection of column vector A onto the column space of matrix B is the unique vector P satisfying B.'*(P-A)=0. The demo below shows that the procedure from above with Q produces this point and also agrees with the more usual least squares solution for the projection B*(B\A).
B=rand(5,3);
A=rand(5,1);
[Q,R]=qr(B,0);
Q=Q.*sign(diag(R)');
P1=Q*Q.'*A
P1 = 5×1
0.5921
0.7305
0.5290
0.2955
0.2067
P2=B*(B\A)
P2 = 5×1
0.5921
0.7305
0.5290
0.2955
0.2067
%Orthogonality test
B'*(A-P1)
ans = 3×1
1.0e-15 *
-0.5829
-0.7772
-0.3331
Torsten
2023-1-26
编辑:Torsten
2023-1-26
A = rand(5,3);
B = rand(5,1);
[Q,R]=mgs(B);
ProjA=Q*Q.'*A
ProjA = 5×3
0.0893 0.2064 0.1511
0.0225 0.0520 0.0381
0.4198 0.9709 0.7106
0.1182 0.2734 0.2001
0.0302 0.0698 0.0511
for j = 1:3
(A(:,j).'*B)/(B.'*B) * B
end
ans = 5×1
0.0893
0.0225
0.4198
0.1182
0.0302
ans = 5×1
0.2064
0.0520
0.9709
0.2734
0.0698
ans = 5×1
0.1511
0.0381
0.7106
0.2001
0.0511
function [Q,R] = mgs(X)
% Modified Gram-Schmidt. [Q,R] = mgs(X);
% G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998.
[n,p] = size(X);
Q = zeros(n,p);
R = zeros(p,p);
for k = 1:p
Q(:,k) = X(:,k);
for i = 1:k-1
R(i,k) = Q(:,i)'*Q(:,k);
Q(:,k) = Q(:,k) - R(i,k)*Q(:,i);
end
R(k,k) = norm(Q(:,k))';
Q(:,k) = Q(:,k)/R(k,k);
end
end
Torsten
2023-1-26
I'm surprised you now found what you were searching for.
In the end, was it projecting a single vector onto a set of vectors or a set of vectors onto a single vector you were aiming at ?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Algebra 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)