Hi All,

I am new to the Arduino world, and have a quick question! I am a Matlab user and quite lazy in my coding, trying to use as many shortcuts as possible!

In Matlab if I wanted to create an array length 5 between 0 and 10 I could use array = linspace(0,10,5) or array = [0:2:10]. Is there anything like this in the Arduino world?

I am trying to use a loop with 9 items and don't want to type out (0,1,2,3....,8) each time essentially.

Jake

It's not clear exactly what you're trying to accomplish but I'll guess the answer is for:

In Matlab if I wanted to create an array length 5 between 0 and 10 I could use array = linspace(0,10,5)

I am not familiar with Matlab. Would the command you quote create the array or would it need to have been previously created/declared ?

Can you give an example of what would be in the array after creating it in Matlab ?

C++ has no built in commands to do what you want but if you can give details of what is required it should be easy to write a function to do it.

``````int arrayInt[10] = {0,0,0,0,0,0,0,0,0,0};
``````

{0,0,0,0,0,0,0,0,0,0} is a shortcut in C for initialising the elements of an array at the point where you declare the array. But it would be rather impractical if your array had 100 elements for example.

As for iterating through the array elements:

``````for {int nI = 0; nI < sizeof arrayInt; n++}
{
Serial.println(arrayInt[n]);
}
``````

If you use 'sizeof arrayInt' then you won't need to change the for loop code everytime you change your mind as to how many elements arrayInt should have.

If you are going to only use elements 5-10 of your 10-element array then that's an unncessary waste of space. The Arduino only has a very small memory and just storing zeros in the first half of the array is going to run out of memory as soon as the array gets large.

However, for small arrays like your example, it's OK. You probably aren't going to fill the Arduino's memory with that.

The OP says in Matlab he would use

``````array = linspace(0,10,5)
``````

``````int arrayInt[10] = {0,0,0,0,0,0,0,0,0,0};
``````for {int nI = 0; nI < sizeof arrayInt; n++}