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

标签 c++ boost vector matrix

  • 我正在尝试将矩阵行复制到 vector 的 vector 中,我该如何做 应用它吗?
  • 是否可以将 boost 矩阵转换为 vector 的 vector ?

源代码:

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

}

最佳答案

您不能将 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);
}

关于c++ - 将 boost::numeric::ublas::matrix 行复制到 vector 的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22838385/

相关文章:

c++ - 用于 C++ 的 MySQL 连接器 | setSchema 上的 MySQL_Connection::setReadOnly() 异常

包括来自其他目录的 C++ 不起作用

c++ - boost 序列化段错误

c++ - 生成一个 boost-python 引用

java - LibGdx 鼠标位置相对于正交相机而不是屏幕

c++ - 如何添加一个多位数的 int 和一个 char,输出将是 int + char

c++ - 访问所有点的最短时间 : Understanding

带N个参数的C++方法

c++ - 用于恢复先前值的 RAII 对象

c++ - 将文本转换为 HPGL(绘图仪) vector 坐标