이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
 - 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
 
Projection using Modified Gram-Schmidt orthogonality
    조회 수: 15 (최근 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개)
참고 항목
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 (한국어)
 
