与此问题matrix that forms an orthogonal basis with a given vector类似的问题,但我的实施存在问题。我有一个给定的向量,u
,我想构造一个任意的正交矩阵V
,使V(:,1) = u/norm(u)
。我得到的问题是V'*V
不是I
(而是对角线)。这是我的代码:
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
答案 0 :(得分:1)
完成正交化后,只需标准化V
的列。请注意,最初也不需要对u
(第一列)进行标准化,因为我们将在最后对整个矩阵进行标准化:
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