C++ Armadillo: mat initialization from vector<vector<double>> destroys data -


i'm trying initialize data vector of vectors armadillo mat.

found site explaining how it:

http://www.kaiyin.co.vu/2014/07/initialize-armadillo-matrix-with-std.html

after printing vector of vectors , resulting mat came conclusion armadillo makes garbage of data.

the main part of function:

 vector<vector<double>> c_mat(num_of_functions,vector<double>(num_of_points));         for(int j=0; j< num_of_functions; ++j)         {             for( int i=0;i<num_of_points; ++i)             {                 c_mat[j][i]=(functions[j](points[i].x()));// shouldn't bother you, works.              }         }         mat c(&(c_mat.front()).front(),num_of_points, num_of_functions);          cout << c << endl << endl;         for(int i=0; i< num_of_functions;++i)         {             print_vec(c_mat[i]);         } 

print_ve(vector) function wrote prints vector.

output:

    1.0000e+00        0e+00   1.9965e+01     1.0000e+00  4.7924e-322   1.2822e+00     1.0000e+00   1.1683e+01        0e+00     1.0000e+00   1.6936e+01  4.7924e-322     1.0000e+00   2.3361e-01   1.0237e+02     1.0000e+00   1.6445e+01   2.1512e+02     1.0000e+00   7.4271e+00   4.0931e-02     1.0000e+00   1.4162e+01   2.0284e+02     1.0000e+00   1.1670e+01   4.1371e+01     1.0000e+00   2.3633e+00   1.5042e+02 

printing vector:

1 1 1 1 1 1 1 1 1 1

printing vector:

11.6828 16.9359 0.233613 16.4455 7.42708 14.1619 11.6701 2.36329 19.9653 1.28223

printing vector:

102.366 215.119 0.0409313 202.84 41.3711 150.421 102.143 4.18885 298.959 1.23308

update:

i tried changing code respect first comment making own 2d vector out of 1d vector. armadillo not destroy data scrambles it:

        vector<double> c_mat(num_of_functions*num_of_points);            for( int i=0;i<num_of_points; ++i)         {             for(int j=0; j< num_of_functions; ++j)             {                     c_mat[i*num_of_functions+j]= functions[j](points[i].x());             }         }          mat c(&c_mat.front(),num_of_points, num_of_functions); 

output:

     1.0000e+00   1.1170e+01   1.6853e+01      4.5538e+00   9.3574e+01   1.0000e+00      1.5553e+01   1.0000e+00   1.6292e+01      1.0000e+00   8.7956e-01   1.9907e+02      6.0653e+00   5.8021e-01   1.0000e+00      2.7591e+01   1.0000e+00   1.0787e+01      1.0000e+00   1.8169e+01   8.7269e+01      1.3849e+01   2.4758e+02   1.0000e+00      1.4385e+02   1.0000e+00   1.7998e+01      1.0000e+00   4.7403e+00   2.4295e+02      1  4.5538  15.5528     1  6.06532  27.5911     1  13.8494  143.854     1  11.1698  93.574     1  0.879557  0.580215     1  18.1687  247.577     1  4.74031  16.8529     1  16.2919  199.069     1  10.787  87.2695     1  17.998  242.945  

an std::vector equivalient data pointer , length (and irrelevant stuff). pointer points bunch of memory, actual elements stored. 0th element stored @ address pointer, nth element stored @ pointer+n. why armadillo mat constructor can take adress of first element , length , construct matrix.

when make vector num_of_functions inner vectors, each own data pointer. means data not stored in contiguous memory.

to initialize 2-d matrix std::vector should create matrix size num_of_functions*num_of_points , store values in column-major-ordering.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -