Using a variable vector in a loop

2 views (last 30 days)
FW on 3 Dec 2021
Commented: FW on 4 Dec 2021
Hello, I am trying to use a large number of vectors (up to 60) for orthogonalization. Say, we have column vectors, v1, v2, v3,..., vk. To orthogonalize them using the standard method, one can proceed with
and so on. However, I want to use the generalized formula when the number of vectors gets large.
What would be best way to implement the above general formula rather? I was wondering if a "for" loop might work, as follows, but how to define vk? I get an error. vk undefined. Thanks.
for k=1:60;

Accepted Answer

James Tursa
James Tursa on 3 Dec 2021
Suppose you store the column vectors in a matrix called V. Then vk would simply be V(:,k). And if the uj are stored in a matrix U as well, then each uj would be U(:,j). So taking your code and replacing these, along with coding the inner part as a loop, would be something like:
V = your matrix of vk column vectors
[m,n] = size(V);
U = zeros(m,n);
for k=1:n
p = zeros(m,1);
for j=1:k-1
p = p + (dot(V(:,k),U(:,j))/dot(U(:,j),U(:,j))) * U(:,j);
U(:,k) = V(:,k) - p;
  1 Comment
FW on 4 Dec 2021
Thank you. I will try this approach. I gather that the columns of U will be populated in each cycle? We need to generate orthogonal uk vectors from given vk vectors.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 4 Dec 2021
You should not use the classical Gram-Schmidt procedure, as it is numerically unstable. Use orth() instead.
FW on 4 Dec 2021
The output of the orth is completely different from the classical GS. However the vectors are orthonormal.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by