How to create a vector from 1: n in C ++ (Armadillo)?

Such a simple question, but I didn't find an answer in the armadillo documentation.

I'm looking for the Armadillo / C ++ equivalent for Matlab x = (1:n)

, where n

is a number and x

is a vector [1, 2, 3..., n-1, n]



source to share

2 answers

Pay attention to this feature .

vec v = linspace<vec>(1, N);


Creates a vector starting at 1 and ending in N. It does exactly what you need.



Assuming C ++ 11 is acceptable and you are using std::vector

, you can use std::iota


std::vector<int> x(n);
std::iota(x.begin(), x.end(), 1);




All Articles