简体   繁体   中英

How to find all occurrences of an item in a ublas::matrix

I am currently working on a algorithm that needs to find all equal occurrences a an item in a matrix. I decided to use uBLAS matrices from boost. So my problem is:

I have a ublas::matrix looking like:

1 2 3 4 5
2 4 6 8 1
1 5 4 6 8
9 4 6 7 0

and I want to find all positions (x,y) of ie the value 6. Is there a function for?

There is no ublas-specific function (as far as I can tell), you will have to scan the matrix the usual way -- through iterators or through indexed access:

typedef std::vector<std::pair<size_t, size_t> > posvec_t;
template <typename T>
posvec_t find_all(const ublas::matrix<T>& m, T val)
{
    posvec_t ret;
    for(size_t r=0; r<m.size1(); ++r)
       for(size_t c=0; c<m.size2(); ++c)
           if(m(r,c) == val)
               ret.push_back( std::make_pair(r, c) );
    return ret;
}

test: https://ideone.com/qhW9b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM