Boost logo

Ublas :

Subject: Re: [ublas] Accessing the indices of non-zero elements in a UBLAS sparse vector
From: Oswin Krause (Oswin.Krause_at_[hidden])
Date: 2011-09-21 13:10:25


Hi Tarek,

the normal iterator iterates over all elements (it assumes that you want
to change all elements and not only the non-zeros).

If you only want to change the non-zero elements you have to write
something like this:

mapped_vector<double> sparseVec;
//...
for(mapped_vector<double>::const_iterator pos = sparseVec.begin(); pos !=
sparseVec.end();++pos){
sparseVec(pos.index()) = 1;
}

By default you should never use mapped_vector<double>::iterator except you
don't need access to all elements!

it's true that this is not well documented. I also found that out only
after long google sessions :)

I hope i could help,

Greetings,
Oswin

> Thanks Oswin,
> I think this should be included in the documentation, (See
> here<http://stackoverflow.com/q/7501996/894755>).
> Beginners like me can't find this easily.
> I didn't understand your note about const_iterator.
>
> Regards,
> Tarek
>
> On Wed, Sep 21, 2011 at 6:47 PM, Oswin Krause <
> Oswin.Krause_at_[hidden]> wrote:
>>> Hello,
>>> How can I know the index of the first non-zero element in a
>>> sparse_vector in ublas and each subsequent nonzero element as well?
>>> The function begin() gives me an iterator that can be used to know the
>>> first non-zero value stored, not its index in the vector.
>>> Tarek
>> Iterators offer a method index() which returns the desired result. But
>> remember that only the const_iterator is really sparse!
>>
>> for(SparseVector::const_iterator pos = sparseVec.begin(); pos !=
>> sparseVec.end();++pos){
>> std::cout << pos.index()<< " "<< *pos;
>> }
>>
>> Greetings,
>> Oswin
>>
>> _______________________________________________
>> ublas mailing list
>> ublas_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/ublas
>> Sent to: t.elsayed_at_[hidden]
>>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: Oswin.Krause_at_[hidden]