octave:2> A=rand(4,3) A = 0.553040 0.419910 0.964850 0.246630 0.351776 0.590348 0.989618 0.563208 0.724291 0.898419 0.050558 0.153609 octave:3> [Q,R]=qr(A) Q = -0.376892 -0.385934 0.771968 -0.336262 -0.168076 -0.518765 0.023574 0.837901 -0.674415 -0.294063 -0.607069 -0.300264 -0.612264 0.703892 0.187020 0.307720 R = -1.46737 -0.62818 -1.04539 0.00000 -0.47458 -0.78348 0.00000 0.00000 0.34778 0.00000 0.00000 0.00000 octave:4> Q*R ans = 0.553040 0.419910 0.964850 0.246630 0.351776 0.590348 0.989618 0.563208 0.724291 0.898419 0.050558 0.153609 octave:5> A A = 0.553040 0.419910 0.964850 0.246630 0.351776 0.590348 0.989618 0.563208 0.724291 0.898419 0.050558 0.153609 octave:6> size(Q) ans = 4 4 octave:7> Q'*Q ans = 1.0000e+00 -1.1102e-16 1.6653e-16 -1.1102e-16 -1.1102e-16 1.0000e+00 5.5511e-17 -1.6653e-16 1.6653e-16 5.5511e-17 1.0000e+00 9.0206e-17 -1.1102e-16 -1.6653e-16 9.0206e-17 1.0000e+00 octave:8> R R = -1.46737 -0.62818 -1.04539 0.00000 -0.47458 -0.78348 0.00000 0.00000 0.34778 0.00000 0.00000 0.00000 octave:9> [Q,R,P]=qr(A) Q = -0.376892 -0.665945 0.548997 -0.336262 -0.168076 -0.483715 -0.188926 0.837901 -0.674415 -0.022474 -0.674167 -0.300264 -0.612264 0.567479 0.456518 0.307720 R = -1.46737 -1.04539 -0.62818 0.00000 -0.85720 -0.43376 0.00000 0.00000 -0.19255 0.00000 0.00000 0.00000 P = Permutation Matrix 1 0 0 0 0 1 0 1 0 octave:10> Q*R ans = 0.553040 0.964850 0.419910 0.246630 0.590348 0.351776 0.989618 0.724291 0.563208 0.898419 0.153609 0.050558 octave:11> A*P ans = 0.553040 0.964850 0.419910 0.246630 0.590348 0.351776 0.989618 0.724291 0.563208 0.898419 0.153609 0.050558 octave:12> help qr 'qr' is a function from the file /usr/lib/x86_64-linux-gnu/octave/3.8.1/oct/x86_64-pc-linux-gnu/qr.oct -- Loadable Function: [Q, R, P] = qr (A) -- Loadable Function: [Q, R, P] = qr (A, '0') -- Loadable Function: [C, R] = qr (A, B) -- Loadable Function: [C, R] = qr (A, B, '0') Compute the QR factorization of A, using standard LAPACK subroutines. For example, given the matrix 'A = [1, 2; 3, 4]', [Q, R] = qr (A) returns Q = -0.31623 -0.94868 -0.94868 0.31623 R = -3.16228 -4.42719 0.00000 -0.63246 The 'qr' factorization has applications in the solution of least squares problems min norm(A x - b) for overdetermined systems of equations (i.e., A is a tall, thin matrix). The QR factorization is 'Q * R = A' where Q is an orthogonal matrix and R is upper triangular. If given a second argument of '0', 'qr' returns an economy-sized QR factorization, omitting zero rows of R and the corresponding columns of Q. If the matrix A is full, the permuted QR factorization '[Q, R, P] = qr (A)' forms the QR factorization such that the diagonal entries of R are decreasing in magnitude order. For example, given the matrix 'a = [1, 2; 3, 4]', [Q, R, P] = qr (A) returns Q = -0.44721 -0.89443 -0.89443 0.44721 R = -4.47214 -3.13050 0.00000 0.44721 P = 0 1 1 0 The permuted 'qr' factorization '[Q, R, P] = qr (A)' factorization allows the construction of an orthogonal basis of 'span (A)'. If the matrix A is sparse, then compute the sparse QR factorization of A, using CSPARSE. As the matrix Q is in general a full matrix, this function returns the Q-less factorization R of A, such that 'R = chol (A' * A)'. If the final argument is the scalar '0' and the number of rows is larger than the number of columns, then an economy factorization is returned. That is R will have only 'size (A,1)' rows. If an additional matrix B is supplied, then 'qr' returns C, where 'C = Q' * B'. This allows the least squares approximation of 'A \ B' to be calculated as [C, R] = qr (A, B) x = R \ C See also: chol, hess, lu, qz, schur, svd, qrupdate, qrinsert, qrdelete, qrshift. Additional help for built-in functions and operators is available in the online version of the manual. Use the command 'doc ' to search the manual index. Help and information about Octave is also available on the WWW at http://www.octave.org and via the help@octave.org mailing list. octave:13> A=[1,2;3,4] A = 1 2 3 4 octave:14> [Q,R]=qr(A) Q = -0.31623 -0.94868 -0.94868 0.31623 R = -3.16228 -4.42719 0.00000 -0.63246 octave:15> [Q,R,P]=qr(A) Q = -0.44721 -0.89443 -0.89443 0.44721 R = -4.47214 -3.13050 0.00000 0.44721 P = Permutation Matrix 0 1 1 0 octave:16> w=rand(4,1) w = 0.12883 0.87196 0.39809 0.88312 octave:17> w*w' ans = 0.016597 0.112335 0.051286 0.113773 0.112335 0.760314 0.347120 0.770047 0.051286 0.347120 0.158478 0.351564 0.113773 0.770047 0.351564 0.779905 octave:18> repmat(w,1,4) ans = 0.12883 0.12883 0.12883 0.12883 0.87196 0.87196 0.87196 0.87196 0.39809 0.39809 0.39809 0.39809 0.88312 0.88312 0.88312 0.88312 octave:19> repmat(w',4,1) ans = 0.12883 0.87196 0.39809 0.88312 0.12883 0.87196 0.39809 0.88312 0.12883 0.87196 0.39809 0.88312 0.12883 0.87196 0.39809 0.88312 octave:20> repmat(w,1,4).*repmat(w',4,1) ans = 0.016597 0.112335 0.051286 0.113773 0.112335 0.760314 0.347120 0.770047 0.051286 0.347120 0.158478 0.351564 0.113773 0.770047 0.351564 0.779905 octave:21> w*w' ans = 0.016597 0.112335 0.051286 0.113773 0.112335 0.760314 0.347120 0.770047 0.051286 0.347120 0.158478 0.351564 0.113773 0.770047 0.351564 0.779905 octave:22> A=randn(4,3) A = -0.58322 0.76081 -0.59277 -0.53438 -0.53647 1.21362 -1.58790 -0.21484 -0.69315 0.59450 0.46057 0.70019 octave:23> m=4 m = 4 octave:24> x=A(:,1) x = -0.58322 -0.53438 -1.58790 0.59450 octave:25> w(1)=x(1)-norm(x) w = -2.45420 0.87196 0.39809 0.88312 octave:26> w(2:m)=x(2:m) w = -2.45420 -0.53438 -1.58790 0.59450 octave:27> Qw1=eye(m)-2/norm(w)^2*w*w' Qw1 = -0.311717 -0.285614 -0.848700 0.317749 -0.285614 0.937810 -0.184797 0.069187 -0.848700 -0.184797 0.450878 0.205588 0.317749 0.069187 0.205588 0.923029 octave:28> Qw1'*Qw1 ans = 1.0000e+00 5.8981e-17 1.3878e-16 -5.5511e-17 5.8981e-17 1.0000e+00 6.0715e-17 -1.3878e-17 1.3878e-16 6.0715e-17 1.0000e+00 -2.7756e-17 -5.5511e-17 -1.3878e-17 -2.7756e-17 1.0000e+00 octave:29> Qw1*A ans = 1.87098 0.24475 0.64891 -0.00000 -0.64883 1.48398 0.00000 -0.54874 0.11023 0.00000 0.58558 0.39940 octave:30> A A = -0.58322 0.76081 -0.59277 -0.53438 -0.53647 1.21362 -1.58790 -0.21484 -0.69315 0.59450 0.46057 0.70019 octave:31> A=rand(4,3) A = 0.819316 0.852734 0.781403 0.387064 0.356249 0.842849 0.033017 0.827699 0.723771 0.569920 0.040136 0.727477 octave:32> b=rand(4,1) b = 0.50213 0.93465 0.92071 0.54425 octave:33> rank(A) ans = 3 octave:34> rank([A,b]) ans = 4 octave:35> A\b ans = -6.7600e-01 7.8662e-05 1.3446e+00 octave:36> [Q,R]=qr(A); octave:37> R R = -1.07098 -0.82798 -1.31184 0.00000 -0.92478 -0.55005 0.00000 0.00000 0.59209 0.00000 0.00000 0.00000 octave:38> Q'*b ans = -1.039943 -0.739654 0.796105 -0.085289 octave:39> Rn=R(1:n,1:n) error: 'n' undefined near line 1 column 8 error: invalid limit value in colon expression error: evaluating argument list element number 1 octave:39> Rn=R(1:3,1:3) Rn = -1.07098 -0.82798 -1.31184 0.00000 -0.92478 -0.55005 0.00000 0.00000 0.59209 octave:40> bn=Q'*b bn = -1.039943 -0.739654 0.796105 -0.085289 octave:41> bn=bn(1:3) bn = -1.03994 -0.73965 0.79611 octave:42> R\b ans = -0.87716 -1.93559 1.55501 octave:43> Rn\bn ans = -6.7600e-01 7.8662e-05 1.3446e+00 octave:44> A\b ans = -6.7600e-01 7.8662e-05 1.3446e+00 octave:45> Rn Rn = -1.07098 -0.82798 -1.31184 0.00000 -0.92478 -0.55005 0.00000 0.00000 0.59209 octave:46> bn bn = -1.03994 -0.73965 0.79611 octave:47> help svd 'svd' is a built-in function from the file libinterp/corefcn/svd.cc -- Built-in Function: S = svd (A) -- Built-in Function: [U, S, V] = svd (A) -- Built-in Function: [U, S, V] = svd (A, ECON) Compute the singular value decomposition of A A = U*S*V' The function 'svd' normally returns only the vector of singular values. When called with three return values, it computes U, S, and V. For example, svd (hilb (3)) returns ans = 1.4083189 0.1223271 0.0026873 and [u, s, v] = svd (hilb (3)) returns u = -0.82704 0.54745 0.12766 -0.45986 -0.52829 -0.71375 -0.32330 -0.64901 0.68867 s = 1.40832 0.00000 0.00000 0.00000 0.12233 0.00000 0.00000 0.00000 0.00269 v = -0.82704 0.54745 0.12766 -0.45986 -0.52829 -0.71375 -0.32330 -0.64901 0.68867 If given a second argument, 'svd' returns an economy-sized decomposition, eliminating the unnecessary rows or columns of U or V. See also: svd_driver, svds, eig, lu, chol, hess, qr, qz. Additional help for built-in functions and operators is available in the online version of the manual. Use the command 'doc ' to search the manual index. Help and information about Octave is also available on the WWW at http://www.octave.org and via the help@octave.org mailing list. octave:48> [U,S,V]=svd(A) U = -0.6563580 -0.0047491 -0.7518963 -0.0618355 -0.4595759 -0.1691910 0.4630058 -0.7387760 -0.4626120 0.7236311 0.3701449 0.3540352 -0.3794395 -0.6691113 0.2885660 0.5701276 S = Diagonal Matrix 2.10676 0 0 0 0.67228 0 0 0 0.41404 0 0 0 V = -0.44959 -0.63489 -0.62832 -0.53236 0.75529 -0.38227 -0.71726 -0.16263 0.67756 octave:49> d=U'*b d = -1.391565 0.141567 0.553044 -0.085289 octave:50> y=S(1:3,1:3)\d(1:3) y = -0.66052 0.21058 1.33574 octave:51> y=d(1:3)./diag(S) y = -0.66052 0.21058 1.33574 octave:52> x=V*y x = -6.7600e-01 7.8662e-05 1.3446e+00 octave:53> A A = 0.819316 0.852734 0.781403 0.387064 0.356249 0.842849 0.033017 0.827699 0.723771 0.569920 0.040136 0.727477 octave:54> b b = 0.50213 0.93465 0.92071 0.54425 octave:55> A\b ans = -6.7600e-01 7.8662e-05 1.3446e+00 octave:56> A=rand(3,4) A = 0.959020 0.332199 0.711439 0.434000 0.698192 0.092040 0.022382 0.828098 0.032155 0.461787 0.948193 0.814440 octave:57> b=rand(3,1) b = 0.31545 0.50466 0.75629 octave:58> rank(A) ans = 3 octave:59> A\b ans = -0.13411 0.12336 0.13645 0.70510 octave:60> [Q,R,p]=qr(A,0) Q = -0.35002 0.78485 0.51136 -0.66786 0.17369 -0.72373 -0.65685 -0.59484 0.46338 R = -1.23993 -0.82309 -0.88678 -0.48107 0.00000 0.85483 -0.00176 0.00202 0.00000 0.00000 0.78697 0.31724 p = 4 1 3 2 octave:61> [Q,R,P]=qr(A) Q = -0.35002 0.78485 0.51136 -0.66786 0.17369 -0.72373 -0.65685 -0.59484 0.46338 R = -1.23993 -0.82309 -0.88678 -0.48107 0.00000 0.85483 -0.00176 0.00202 0.00000 0.00000 0.78697 0.31724 P = Permutation Matrix 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 0 octave:62> x = R(:,1:size (A, 1)) \ (Q’ * b); error: invalid character 'â' (ASCII 226) near line 1, column 29 parse error: syntax error >>> x = R(:,1:size (A, 1)) \ (Q’ * b); ^ octave:62> x = R(:,1:size (A, 1)) \ (Q' * b); octave:63> x x = 0.71713 -0.13371 0.18617 octave:64> x(size (A, 2)) = 0; octave:65> x(p) = x; octave:66> x x = -0.13371 0.00000 0.18617 0.71713 octave:67> A*x ans = 0.31545 0.50466 0.75629 octave:68> b b = 0.31545 0.50466 0.75629 octave:69> norm(A\b) ans = 0.74093 octave:70> norm(x) ans = 0.75287 octave:71> [1,2]\1 ans = 0.20000 0.40000 octave:72> [1,2;1,2]\[1;1] ans = 0.20000 0.40000 octave:73> [1,2;1,2]\[1;2] ans = 0.30000 0.60000 octave:74> [1,2;1,2;1,2]\[1;2;1] ans = 0.26667 0.53333 octave:75> A=[1,2;3,4] A = 1 2 3 4 octave:76> b=[1;2] b = 1 2 octave:77> A\b ans = 0.00000 0.50000 octave:78> A=[1,2;3,4;1,2] A = 1 2 3 4 1 2 octave:79> b=[1;2;1] b = 1 2 1 octave:80> A\b ans = 1.8411e-16 5.0000e-01 octave:81> A=[1,0;0,eps] A = 1.00000 0.00000 0.00000 0.00000 octave:82> format e error: format: unrecognized format state 'e' octave:82> format short octave:83> A=[1,0;0,eps] A = 1.00000 0.00000 0.00000 0.00000 octave:84> format long e octave:85> A=[1,0;0,eps] A = 1.00000000000000e+00 0.00000000000000e+00 0.00000000000000e+00 2.22044604925031e-16 octave:86> diary off