1function basis = basis_col(A)
2%
3% Bases
4%
5% basis_col(A) produces a basis for the subspace of Eucldiean n-space
6% spanned by the vectors {u1,u2,...}, where the matrix A is formed from
7% these vectors as its columns. That is, the subspace is the column space
8% of A. The columns of the matrix that is returned are the basis vectors
9% for the subspace. These basis vectors will be a subset of the original
10% vectors. An error is returned if a basis for the zero vector space is
11% attempted to be produced.
12%
13% For example, if the vector space V = span{u1,u2,...}, where u1,u2,... are
14% row vectors, then set A to be [u1' u2' ...].
15%
16% For example, if the vector space V = Col(B), where B is an m x n matrix,
17% then set A to be equal to B.
18%
19%  Function written by Anthony Russo, downloaded from MatlabCentral.
20%
21
22matrix_size = size(A);
23
24m = matrix_size(1,1);
25n = matrix_size(1,2);
26
27if A == zeros(m,n)
28    error('There does not exist a basis for the zero vector space.');
29elseif n == 1
30    basis = A;
31else
32    flag = 0;
33
34    if n == 2
35        multiple = A(1,2)/A(1,1);
36        count = 0;
37
38        for i = 1:m
39            if A(i,2)/A(i,1) == multiple
40                count = count + 1;
41            end
42        end
43
44        if count == m
45            basis = A(1:m,1);
46            flag = 1;
47        end
48    end
49
50    if flag == 0
51        [ref_A pivot_columns] = ref(A);
52
53        for i = 1:size(pivot_columns,2)
54            B(1:m,i) = A(1:m,pivot_columns(1,i));
55        end
56
57        basis = B;
58    end
59end
60