Boost logo

Ublas :

Subject: Re: [ublas] Distinct rows in a matrix
From: Gunter Winkler (guwi17_at_[hidden])
Date: 2008-11-01 15:46:55


Am Freitag, 31. Oktober 2008 10:41 schrieb Paweł Szlendak:
> Unfortunatelly, linear independence is not what I want to check.
> Since, having
>
> A = [ 1 2 3; 4 5 6 ; 2 4 6]
>
> rank(A) = 2 and there are 3 distict rows although row 1 is a linear
> combination of row 3.
> What I am trying to obtain is something like SQL query:
>
> select distinct *
> from A

Then you could order the rows of the matrix and make the result unique.

This can be achieved by using a "permutation vector" and custom compare
functors:

// pseudo code

class row_compare {
 const matrix & A;
 row_compare(const matrix & A) : A(A) {}
 boolean operator() (int left, int right) const {
   return row(A, left) < row(A, right);
 }
};

class row_equal {
 const matrix & A;
 row_compare(const matrix & A) : A(A) {}
 boolean operator() (int left, int right) const {
   return row(A, left) == row(A, right);
 }
};

vector<int> index(A.size1());
... foreach i: set index[i] = i

std::sort(index.begin(), index.end(), row_compare(A));

iterator new_end = std::unique(index.begin(), index.end(),
row_equal(A));

mfg
Gunter