Armadillo, how to grow a vector and get his size?

11,837

See Armadillo's documentation. For example, you can use X.n_elem to get the length of vector X. To resize a vector while preserving the data, use .resize(). To add a row or column to a matrix, use .insert_rows() or .insert_cols().

Note that resizing objects inside performance critical loops (whether they're Armadillo matrices/vectors or std::vector) is not efficient. It's far better to work out the correct size beforehand.

Share:
11,837
The Unholy Metal Machine
Author by

The Unholy Metal Machine

(your about me is currently more black than black itself) (I make my own booze) I'm human, that means : sometimes I have a bad temper, I can do shit, I am biased, I show contradictions... ... but I do like a God! (Genesis 1:27 So God created mankind in his own image)

Updated on June 04, 2022

Comments

  • The Unholy Metal Machine
    The Unholy Metal Machine about 2 years

    In my last question, Ilmari Karonen propose to me to solve the Laplace equation in discret mode. I'm using a grid and the four closest neighbours at each node to compute the unknown heights.

    enter image description here

    So the system to solve can be written with matrix :

    A(matrix nxn) * U(vector n, unknowns) = b(vector n, settled)

    last evening I looked around the web about linear algebra for C++. My choice is Armadillo. But for now "U" and "b" are std::map :

    std::map<std::pair<float,float>,float> b;
    std::map<std::pair<float,float>,float> U;
    .
    .
    .
    if(g/*grid*/->getNode(r,c)->status == TO_COMPUTE){
              U.insert(std::make_pair(std::make_pair(r,c),g->getNode(r,c)->y));
              /*right*/ if(g->getNode(r+1,c)->status == SETTLED) b.at(std::make_pair(r,c)) += g >getNode(r+1,c)->y;
              /*left */ if(g->getNode(r-1,c)->status == SETTLED) b.at(std::make_pair(r,c)) += g->getNode(r-1,c)->y;
              /*down */ if(g->getNode(r,c+1)->status == SETTLED) b.at(std::make_pair(r,c)) += g->getNode(r,c+1)->y;
              /*up   */ if(g->getNode(r,c-1)->status == SETTLED) b.at(std::make_pair(r,c)) += g->getNode(r,c-1)->y;
            }
    

    and I think I will have then to get the size of "U" to create arma::vec and to parse the whole map with an std::iterator to transfer the values in my arma::vec. So I would like to know how to grow a arma::vec I'm looking for something like std::vector::push_back(), then I will replace my std::map. Also how can I get the size for a arma::vec?

    note1 : the picture shows the contours and the points where I will compute the values, the grid with unknow value is at y=0, when the lower contour is at y=-0.2 and the upper contour at y=0.8

    note2 : when my little (LINUX) program will be ready I will release the code on my bitbucket as a very tiny example of use of discret Laplace operator

    Update 26 Nov. 2013:

    you can get the link to the code here