Boost logo

Ublas :

From: James Sutherland (James.Sutherland_at_[hidden])
Date: 2008-07-21 12:08:59


>
> Hi,
> I am using sparse matrix to store size 2 vectors and I am facing a
> little problem.
> I want just to, let's say, dsplay element of the matrix only if they
> exist, but I couldn't find any method to access non zero element
> indexes or to make existency check. Am I wrong, does such methods
> exist ?
> Here is a sample code:
> ___________________________
> void TestProxies::testSparseMatrix2() {
> using namespace boost::numeric::ublas;
> compressed_matrix<bounded_vector<double, 2> > m(3,3,2);
> bounded_vector<double, 2> v1, v2;
>
> v1(0) = 1.1;
> v1(1) = 2.1;
> v2(0) = 2.13;
> v2(1) = 3.14;
> m(0,0) = v1;
> m(2,1) = v2;
> std::cout << m << std::endl;
> for (unsigned i = 0; i < m.size1 (); ++ i) {
> for (unsigned j = 0; j < m.size2 (); ++ j) {
> if(m(i,j)) {//it would worked fine if m elements were double and
> not vectors
> std::cout << (bounded_vector<double, 2>) m(i,j) << std::endl;
> }
> }
> }

use matrix_row:
   matrix_row row(m,rownumber);
Then you can iterate through the columns.
   for( matrix_row::iterator col=row.begin(); col!=row.end() ){
     // use the col iterator however you want.
   }

James