Reputation: 51
Similar problem to this question matrix that forms an orthogonal basis with a given vector but I'm having problems with my implementation. I have a given vector, u
, and I want to construct an arbitrary orthonormal matrix V
such that V(:,1) = u/norm(u)
. The problem I'm getting is that V'*V
isn't I
(but is diagonal). Here's my code:
function V = rand_orth(u)
n = length(u);
V = [u/norm(u) rand(n,n-1)];
for i = 1:n
vi = V(:,i);
rii = norm(vi);
qi = vi/rii;
for j = i+1:n
rij = dot(qi,V(:,j));
V(:,j) = V(:,j) - rij*qi;
end
end
end
Upvotes: 1
Views: 65
Reputation: 4077
Simply normalize columns of V
once you've finished the orthogonalization. Note there's also no need to normalize u
(first column) initially, since we will normalize the whole matrix at the end:
function V = rand_orth(u)
n = length(u);
V = [u, rand(n,n-1)];
for i = 1:n
vi = V(:,i);
rii = norm(vi);
qi = vi/rii;
for j = i+1:n
rij = dot(qi,V(:,j));
V(:,j) = V(:,j) - rij*qi;
end
end
V = bsxfun(@rdivide, V, sqrt(diag(V'*V))');
end
Upvotes: 1