Question

How can I push an element at the end of vector in vec of armadillo? I am performing adding and removing an element in a sorted list in a loop. This is very expensive thing. The way I am currently doing in case of removing an element from a vec x to vec x_curr as:

x_curr = x(find(x != element))

However its not trivial in case of adding an element in loop.

x_curr = x; x_curr << element; x_curr = sort(x_curr);

This not correct. In addition not very efficient. What would be most efficient way to do this in armadillo. Any other STL library solution. I am using this in Rcpp armadillo. I can perhaps sorting every loop. x_curr is used to store of indices of column of arma::mat i.e. I am going to use it as mat.col(x_curr).

Was it helpful?

Solution

I don't understand your question.

Armadillo is a math library, so it operates on vectors. If you do not know your size, you could allocate a guessed N elements and resize in the common 'times two' idiom as needed, and shrink at the end. If you know the size, well then you have no problem.

The STL has the so-called generic containers and algorithms, but it does not do linear algebra. You need to figure out what you need most, and plan your implementation accordingly.

OTHER TIPS

I am not sure that I understood what you want to do, but if you want to append an element at the end of your vector, you can do it like this:

    int sz = yourvector.size();
    yourvector.resize(sz+1);
    yourvector(sz) = element;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top