現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Projection using Modified Gram-Schmidt orthogonality
7 ビュー (過去 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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)