Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 853
rozyang Avatar asked May 22 '18 13:05

rozyang


People also ask

How to combine 2 vector?

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.

How to merge 2 vectors in R?

To concatenate a vector or multiple vectors in R use c() function. This c() function is used to combine the objects by taking two or multiple vectors as input and returning the vector with the combined elements from all vectors.

What is vector concatenation?

The Vector Concatenate and Matrix Concatenate blocks concatenate input signals to create a nonscalar signal that you can iteratively process with a subsystem, for example, a for-each, while-iterator, or for-iterator subsystem. In the Simulink® library, these blocks are different configurations of the same block.


1 Answers

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
like image 91
Claes Rolen Avatar answered Sep 21 '22 00:09

Claes Rolen