function [ROW,N,COL,LN,E, r] = fourbase(A)

% fourbase  Bases for all 4 fundamental subspaces. 
% 
% [ROW, N, COL, LN] = fourbase(A) returns matrices whose columns
% are bases for the row space, nullspace, column space and
% left nullspace of a matrix A.
%
% The bases for all 4 subspaces come from E*A = R.
% The matrix ROW comes from the r pivot rows of R.
% The matrix N comes from the n-r special solutions. 
% The matrix COL contains the r pivot columns of A.
% The matrix LN contains the last m-r rows of E.
% Those m-r rows multiply A to give the m-r zero rows in R.
% Notice that A = COL * ROW' .
%
% See also rowbasis, nulbasis, colbasis, leftnull.

[m,n] = size(A);
[RE,p]= rref([A eye(m)]);
p=p(p<=n);
R=RE(:, 1:n);
E=RE(:, (n+1):(m+n));
r=rank(A);
ROW=R(1:r, :)'; 
N=null(R,'r');
COL=A(:,p);
LN=E((r+1):m,:)';



