简体   繁体   English

将boost :: numeric :: ublas :: matrix行复制到vector的vector

[英]copy boost::numeric::ublas::matrix row to vector of vectors

  • I am trying to copy matrix rows to a vector of vectors how can I apply it? 我正在尝试将矩阵行复制到向量的向量中,该如何应用呢?
  • Is it possible to convert boost matrix to vector of vectors? 是否可以将Boost矩阵转换为向量的向量?

source code: 源代码:

#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;

}

You can't assign a matrix_row to a vector , it need some conversion: 您不能将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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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