# Spnull

 Other toolboxes required spnull Returns a sparse orthonormal basis for the null space none sporth Helper functions license_spnull.txt
 This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function.

spnull is a function that computes an orthonormal basis for the null space of a full or sparse matrix. When the matrix is sparse, this computation is performed via the QR decomposition and is typically much faster than using null(full(S)).

## Syntax

• Z = spnull(S)
• Z = spnull(S,varargin)

## Argument descriptions

• S: The matrix to have its null space computed.
• varargin (optional): Extra arguments that, if S is full, will be passed to MATLAB's null function.

## Examples

The following example gives a 4-by-4 matrix whose null space is spanned by the two vectors $[0,0,1,0]^T$ and $[0,0,0,1]^T$:

>> S = sparse(4,4);
>> S(1,1) = 1; S(3,2) = 1;
>> spnull(S)

ans =

(3,1)        1
(4,2)        1

Note that the output is sparse because S is sparse. If the input is full then the output will be full as well:

>> spnull(full(S))

ans =

0     0
0     0
1     0
0     1

## Source code

Click on "expand" to the right to view the MATLAB source code for this function.

1. function Z = spnull(S, varargin)
2. % Z = SPNULL(S)
3. % returns a sparse orthonormal basis for the null space of S, that is,
4. % S*Z has negligible elements, and Z'*Z = I
5. %
6. % If S is sparse, Z is obtained from the QR decomposition.
7. % Otherwise, Z is obtained from the SVD decomposition
8. %
9. % Bruno Luong <brunoluong@yahoo.com>
10. % History
11. %   10-May-2010: original version
12. %
13. % See also SPORTH, NULL, QR, SVD, ORTH, RANK
14. 
15. if issparse(S)
16.     [m n] = size(S);
17.     try
18.         [Q R E] = qr(S.'); %#ok %full QR
19.         if m > 1
20.             s = diag(R);
21.         elseif m == 1
22.             s = R(1);
23.         else
24.             s = 0;
25.         end
26.         s = abs(s);
27.         tol = norm(S,'fro') * eps(class(S));
28.         r = sum(s > tol);
29.         Z = Q(:,r+1:n);
30.     catch %#ok
31.         % sparse QR is not available on old Matlab versions
32.         err = lasterror(); %#ok
33.         if strcmp(err.identifier, 'MATLAB:maxlhs')
34.             Z = null(full(S), varargin{:});
35.         else
36.             rethrow(err);
37.         end
38.     end
39. else % Full matrix
40.     Z = null(S, varargin{:});
41. end
42. 
43. end