Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a matrix in dlib to a std::vector

Tags:

c++

dlib

I have a colume vector defined in dlib. How can I convert it to 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

like image 462
colddie Avatar asked Aug 04 '16 12:08

colddie


2 Answers

There are many ways. You could copy it via a for loop. Or use the std::vector constructor that takes iterators: std::vector<double> x(starting_point.begin(), starting_point.end()).

like image 90
Davis King Avatar answered Oct 18 '22 06:10

Davis King


This would be the way you normally iterate over the matrix (doesn't matter if the matrix has only 1 column):

// 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? 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;
    }
}
like image 1
Vincent Olivert Riera Avatar answered Oct 18 '22 04:10

Vincent Olivert Riera