Anne
Anne

Reputation: 261

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

Such a simple question but I haven't found an answer in the armadillo's documentation.

I'm looking for the Armadillo/C++ equivalent to Matlab's x = (1:n) where n is a number and x is thus a vector [1, 2, 3..., n-1, n].

Upvotes: 8

Views: 3470

Answers (2)

grzkv
grzkv

Reputation: 2619

Please, pay attention to this function.

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

Generates a vector starting at 1 and ending at N. It does just what you need.

Upvotes: 7

TartanLlama
TartanLlama

Reputation: 65620

Assuming that 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);

Upvotes: 4

Related Questions