簡體   English   中英

如何將dlib中的矩陣轉換為std :: vector

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

我有一個在dlib中定義的colume向量。 我怎樣才能將它轉換為std :: vector?

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

謝謝

有很多方法。 你可以通過for循環復制它。 或者使用帶有迭代器的std :: vector構造函數: std::vector<double> x(starting_point.begin(), starting_point.end())

這將是您通常迭代矩陣的方式(如果矩陣只有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
    }   
}

那么,為什么不迭代你的列向量並將每個值引入新的std::vector 這是一個完整的例子:

#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