I'm going to assume that your vectors are all n
-dimensional, and that we can concatenate them all into a single matrix. If we go into matrix and linear algebra, what you are looking for is the Column Space of a matrix. Simply put, the column space is defined as the set of columns in your matrix that can uniquely produce another vector in n
-dimensional space. Or, it is the set of all possible linear combinations of the column vectors. As such, if you want to find the largest set of linearly independent vectors, all you have to do is determine what the column space of your matrix is.
Therefore, given your matrix V
which is of size n x m
, where we have m
columns / vectors, with each column being of size n x 1
(or n
rows), you would call the rref
or the Row-Reduced Echelon Form (RREF) command. This reduces your matrix down to its row-reduced echelon form. This is the beginning of finding the column space for a matrix. You would call it like so:
[R, RB] = rref(V);
R
would contain the RREF form of V
and RB
would contain the indices or column numbers of R
that form the column space. Therefore, if you want to produce your linearly independent vectors, you simply have to do:
VMax = V(:,RB);
VMax
will contain only those columns of V
that formed the column space, and hence those that are linearly independent vectors. If you want to determine how many independent vectors we have, you simply have to count how many values of RB
we have:
r = numel(RB);
Here's a quick example to illustrate my point. Supposing I have this matrix:
>> V = [1 1 2 0; 2 2 4 9; 3 3 6 7; 4 4 8 3]
V =
1 1 2 0
2 2 4 9
3 3 6 7
4 4 8 3
The second column / vector is simply the first vector. The third column / vector is simply the first vector plus the second vector, or it could be twice the first or second vector. Either way, this is not a linearly independent vector as this is based off of the first vector. The same goes with the second vector. The last vector is independent of the other three as there is no way we can create combinations or a scaling that can produce this last vector from the other three. If we called rref
, this is what will happen:
>> [R, RB] = rref(V)
R =
1 1 2 0
0 0 0 1
0 0 0 0
0 0 0 0
RB =
1 4
R
contains the row reduced echelon form, while RB
tells us which columns are linearly independent or form the column space of A
. As you can see, only columns 1 and 4 are linearly independent, which makes perfect sense. If you also take a look at the last two rows of R
, we can see that they consist of all zeroes. This alludes to the rank of a matrix, where it is simply the total number of rows (or columns depending on what you're doing) that are non-zero. This also tells you how many vectors form the column space.
To complete your task, simply do:
>> VMax = V(:,RB)
VMax =
1 0
2 9
3 7
4 3
As you can see, each column of VMax
is a linearly independent vector from V
, which also forms the column space of V
.
Now, your next task is to randomly choose linearly independent vectors from this column space each time you run the algorithm. Bear in mind that because there are multiple ways to produce the column space, the solution by rref
will only give you one such column space. If I am interpreting your question correctly, you wish to generate random column spaces and choose a subset of those vectors each time. Thanks to Luis Mendo (and also a gentle prod from knedlsepp), what you can do is randomly rearrange or permute the columns, then run rref
on this permuted matrix.
As such, you can do something like this:
ind = randperm(size(V,2));
Vperm = V(:,ind);
[R,RB] = rref(Vperm);
VMax = Vperm(:,RB);
randperm
will generate a random permutation array from 1 up to the argument that you specify to randperm
. In our case, this is the total number of columns in your matrix V
. We use this array to randomly shuffle the columns of V
, store this into Vperm
and run our code that we have done before. By doing this random shuffling, you are biasing the input to rref
so that you are forcing it to choose different basis vectors, but if you have several vectors that are linearly dependent, there will be a case where we will choose one of these linearly dependent vectors to build our basis.