SHARE
TWEET

QR

alva1515 Nov 2nd, 2018 168 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function [Q,R] = factQR (A)
  2.    
  3.     [nRows,nColumns] = size(A)
  4.     // Si las columnas de A no son LI o no es cuadrada
  5.     if nColumns > rank(A) then
  6.         mprintf("Matriz no valida, retornando");
  7.         return
  8.     end
  9.     Q = zeros([nRows,nColumns])
  10.     R = zeros([nColumns,nColumns])
  11.     // Calculamos las columnas de Q como la base ortonormal de Col(A)
  12.     // Primero aplicamos Gram-Schmidt para obtener la base ortogonal
  13.     for i=1:nColumns
  14.         Q(1:nRows,i) = A(1:nRows,i)
  15.         for j=1:i-1
  16.             Q(1:nRows,i) = Q(1:nRows,i) - (Q(1:nRows,j)' * A(1:nRows,i)) * Q(1:nRows,j)
  17.         end
  18.         Q(1:nRows,i) = Q(1:nRows,i) / norm(Q(1:nRows,i))
  19.     end
  20.     // Sean U = [U1, ... , Un] y A = [A1, ... , An] obtenemos R como
  21.     // una matriz triangular superior donde R(i.j) = <Ui,Aj>
  22.     for i=1:nColumns
  23.         for j=i:nColumns
  24.             R(i,j) = Q(1:nRows,i)' * A(1:nRows,j)
  25.         end
  26.     end
  27.    
  28. endfunction
  29.  
  30. A = [0,1,1;1,1,2;0,0,3]
  31. B = [1,0,-1;1,0,-3;0,1,1;0,-1,1]
  32. [Q,R] = factQR(B)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top