**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Projection using Modified Gram-Schmidt orthogonality

19 views (last 30 days)

Show older comments

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 Comments

Torsten
on 25 Jan 2023

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
on 25 Jan 2023

@Torsten see in the following link :

"Methods for performing orthogonalization include:

- Gram–Schmidt process, which uses projection"

M
on 25 Jan 2023

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
on 25 Jan 2023

Edited: Torsten
on 25 Jan 2023

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
on 25 Jan 2023

Edited: M
on 25 Jan 2023

@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
on 25 Jan 2023

Edited: Torsten
on 25 Jan 2023

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
on 25 Jan 2023

Edited: M
on 25 Jan 2023

@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
on 25 Jan 2023

Edited: Torsten
on 25 Jan 2023

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
on 25 Jan 2023

@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
on 25 Jan 2023

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
on 25 Jan 2023

Torsten
on 25 Jan 2023

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
on 26 Jan 2023

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
on 26 Jan 2023

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.

### Accepted Answer

Matt J
on 25 Jan 2023

Edited: Matt J
on 26 Jan 2023

Aorth=orth(A); %A orthogonalized

ProjB=Aorth*(Aorth.'*B); %projection of B

##### 38 Comments

Matt J
on 26 Jan 2023

M
on 26 Jan 2023

Edited: M
on 26 Jan 2023

Torsten
on 26 Jan 2023

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
on 26 Jan 2023

Edited: M
on 26 Jan 2023

@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
on 26 Jan 2023

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
on 26 Jan 2023

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
on 26 Jan 2023

Edited: Matt J
on 26 Jan 2023

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
on 26 Jan 2023

@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
on 26 Jan 2023

Edited: M
on 26 Jan 2023

"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
on 26 Jan 2023

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
on 26 Jan 2023

Edited: M
on 26 Jan 2023

M
on 26 Jan 2023

Torsten
on 26 Jan 2023

Edited: Torsten
on 26 Jan 2023

( 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
on 26 Jan 2023

Edited: Matt J
on 26 Jan 2023

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
on 26 Jan 2023

Edited: Torsten
on 26 Jan 2023

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
on 26 Jan 2023

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 ?

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- 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)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)