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

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

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

Q = orth(A)

% Since A is rank deficient, Q contains one fewer column than A.
% Copyright 2012 The MathWorks, Inc.