Skip to content

Instantly share code, notes, and snippets.

@alvgaona
Created March 30, 2024 02:23
Show Gist options
  • Save alvgaona/c87f6b0fb49111b07ff45b1a04e6bede to your computer and use it in GitHub Desktop.
Save alvgaona/c87f6b0fb49111b07ff45b1a04e6bede to your computer and use it in GitHub Desktop.
Householder QR Decomposition
A = [
-1 -1 1;
1 3 3;
-1 -1 5
];
[QQ,RR] = qr(A);
[m, n] = size(A);
for k=1:n
y = A(1+k-1:end,k);
e1 = zeros(m-(k-1), 1);
e1(1) = 1;
w = y + sign(y(1)) * norm(y) * e1;
v = w / norm(w);
if k == 1
H = (eye(size(v,1)) - 2*(v*v'));
A = H*A;
else
H = H * [
eye(k-1) zeros(k-1,m-(k-1));
zeros(m-(k-1),k-1) eye(size(v,1)) - 2*(v*v')
];
A = [
eye(k-1) zeros(k-1,m-(k-1));
zeros(m-(k-1),k-1) eye(size(v,1)) - 2*(v*v')
]*A;
end
end
R = A;
Q = H;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment