Search code examples
vectorconcatenationarmadillo

How to concatenate two or more vectors in Amardillo?


For example, if I have

vec a(3, fill::randu);
vec b(5, fill::randu);

How can I get a new vector c of size 8, where the first three elements are from a and the rest from b?


Solution

  • You can use join_cols(a,b) since vec inherits from mat

    #include<armadillo>
    using namespace arma;
    int main()
    {
         vec a(3, fill::randu);
         vec b(5, fill::randu);
         vec c;
    
         c = join_cols(a,b);
         a.print("a");
         b.print("b");
         c.print("a..b"); 
        return 0;
    }
    

    ...gives the output

    a
       0.8402
       0.3944
       0.7831
    b
       0.7984
       0.9116
       0.1976
       0.3352
       0.7682
    a..b
       0.8402
       0.3944
       0.7831
       0.7984
       0.9116
       0.1976
       0.3352
       0.7682