Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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].

like image 277
Anne Avatar asked Sep 11 '14 14:09

Anne


2 Answers

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.

like image 107
grzkv Avatar answered Nov 17 '22 04:11

grzkv


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);
like image 32
TartanLlama Avatar answered Nov 17 '22 04:11

TartanLlama