开发者

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 i.e. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜