简体   繁体   English

如何将dlib中的矩阵转换为std :: vector

[英]how to convert a matrix in dlib to a std::vector

I have a colume vector defined in dlib. 我有一个在dlib中定义的colume向量。 How can I convert it to std::vector? 我怎样才能将它转换为std :: vector?

typedef dlib::matrix<double,0,1> column_vector;
column_vector starting_point(4);
starting_point = 1,2,3,4;
std::vector x = ??

Thanks 谢谢

There are many ways. 有很多方法。 You could copy it via a for loop. 你可以通过for循环复制它。 Or use the std::vector constructor that takes iterators: std::vector<double> x(starting_point.begin(), starting_point.end()) . 或者使用带有迭代器的std :: vector构造函数: std::vector<double> x(starting_point.begin(), starting_point.end())

This would be the way you normally iterate over the matrix (doesn't matter if the matrix has only 1 column): 这将是您通常迭代矩阵的方式(如果矩阵只有1列,则无关紧要):

// loop over all the rows
for (unsigned int r = 0; r < starting_point.nr(); r += 1) {
    // loop over all the columns
    for (unsigned int c = 0; c < starting_point.nc(); c += 1) {
        // do something here
    }   
}

So, why don't you iterate over your column vector and introduce each value into the new std::vector ? 那么,为什么不迭代你的列向量并将每个值引入新的std::vector Here is a full example: 这是一个完整的例子:

#include <iostream>
#include <dlib/matrix.h>

typedef dlib::matrix<double,0,1> column_vector;

int main() {
    column_vector starting_point(4);
    starting_point = 1,2,3,4;

    std::vector<double> x;

    // loop over the column vector
    for (unsigned int r = 0; r < starting_point.nr(); r += 1) {
        x.push_back(starting_point(r,0));
    }

    for (std::vector<double>::iterator it = x.begin(); it != x.end(); it += 1) {
        std::cout << *it << std::endl;
    }
}

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

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