将boost :: numeric :: ublas :: matrix row复制到向量的向量

时间:2014-04-03 12:52:54

标签: c++ boost vector matrix

  • 我正在尝试将矩阵行复制到矢量矢量中,我该怎么办? 申请吗?
  • 是否可以将boost矩阵转换为矢量矢量?

源代码:

#include <iostream.h>
#include <boost/numeric/ublas/matrix.hpp>
#include <vector>

int main()
{
    //declare vector and matrix
    std::vector<std::vector<float>> document;
    boost::numeric::ublas::matrix<float> data(10,10);
    // fill matrix (any numbers)
    for (size_t i = 0; i < data.size1(); i++)
    {
        for (size_t j = 0; j < data.size2(); j++)
        {
            data(i,j) = i + j ;
        }
    }

    //The problem is here. I want to copy data(j) to document i.e document.push_back(data(j)). How can I copy matrix rows to a vector?
  //I know that it's wrong: document.push_back(data(j)) but that's almost what i am trying to do

//matrix_row<matrix<double> > mr (m, i); allows me to access a row right?

return 0;

}

1 个答案:

答案 0 :(得分:4)

您无法将matrix_row分配给vector,需要进行一些转换:

for(size_t i = 0; i < data.size1(); ++i)
{
    std::vector<float> row(data.size2());
    boost::numeric::ublas::matrix_row<boost::numeric::ublas::matrix<float> > mr (data, i); 

    std::copy(mr.begin(), mr.end(), row.begin());
    document.push_back(row);
}