%% Basis for Full Rank Matrix
% Calculate and verify the orthonormal basis vectors for the range of a full rank matrix.
% Define a matrix and find the rank.

A = [1 0 1;-1 -2 0; 0 1 -1];
r = rank(A)

% Since A is a square matrix of full rank, the orthonormal basis calculated by orth(A) matches the matrix U calculated in the singular value decomposition, [U,S] = svd(A,'econ'). This is because the singular values of A are all nonzero.
%
% Calculate the orthonormal basis for the range of A using orth.

Q = orth(A)

% The number of columns in Q is equal to rank(A). Since A is of full rank, Q and A are the same size.
%
% Verify that the basis, Q, is orthogonal and normalized within a reasonable error range.

E = norm(eye(r)-Q'*Q,'fro')

% The error is on the order of eps.
% Copyright 2012 The MathWorks, Inc.