Take the 2-minute tour ×

how to create this in c++? this is in matlab. I've searched on google but I couldn't find this out. Can someone who knows this help me?

a = linspace(-3000, 3000, 128)

this is a vector from -3000 to 3000 with 128 elements.

share|improve this question
what did you try? there is no such function in c++, you need to write the logic yourself –  Ankur Sep 4 '12 at 11:20
thanks, I tried to use a while, but it didn't work. –  SamuelNLP Sep 4 '12 at 11:22

1 Answer 1

up vote 6 down vote accepted

A crude example:

#include <vector>

std::vector<double> v(128);     // create new vector with 128 elements

for (int i = 0; i < 128; ++i)   // init vector with required values
    v[i] = -3000.0 + 6000.0 * i / 127.0;
share|improve this answer
There's no need for the static_cast. Standard C++ promotion rules already imply that behaviour. –  Kerrek SB Sep 4 '12 at 11:34
It works, thanks. –  SamuelNLP Sep 4 '12 at 11:37
@Kerek SB: yes, good point - I tend to make int<->float casts explicit regardless as a matter of style. I'll edit out the cast in this instance to make the example simpler. –  Paul R Sep 4 '12 at 11:40

Your Answer


