Create array of linearly spaced elements

Hi everyone

May I know if there is a function which can create an array of elements which are equally spaced? Just like MATLAB function "linspace" which creates number of equally spaced elements between two values.

For example, I would like to know if arduino can create an array with 11 equally spaced elements between 0 and 4 which gives us [0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8, 3.2, 3.6, 4.0]. Please help me.

Thank you so much!

Tommy

No.

You need to implement one yourself.

arduino_new:
No.

You need to implement one yourself.

Preferably using a for loop.

And remember that an int divided by an int will yield an int (any fractional part will be silently discarded). But dividing a float by an int, or a float by another float, will work fine.

float x;

x = 9 / 2;
// x is now 4.0

x = 9.0 / 2;
// x is now 4.5

Several possibilities...

For so few values, just initialise the array when it’s defined.
OR, as the members are linearly spaced, simply calculate them when needed.

The difference in speed of these options will make very little difference to your code, and will be faster than a special function to return your value.

If you just need to initialize a table THAT YOU ARE GOING TO MODIFY LATER, the compiler can do the math for you:

float Table[11] = {0*(4.0/10), 1*(4.0/10), 2*(4.0/10), 3*(4.0/10), 4*(4.0/10), 5*(4.0/10), 6*(4.0/10), 7*(4.0/10), 8*(4.0/10), 9*(4.0/10), 10*(4.0/10)};

There is no good reason to have a constant array that is so simple to calculate at run time. Just replace Table[ i ] with i*(4.0/10)