# Creating an array with a sequence of values in Octave

In this lesson I'll explain how to create a **sequence of increasing or decreasing numbers** in an array variable of Octave.

To create a sequence of numbers you have to indicate three parameters: the beginning (START), the increment or decrement (STEP) and the end (STOP) separated from each other.

The step information (increment or decrement) is optional. If it is missing, Octave defaults to a pitch equal to 1. Alternatively, you can also use functions **linspace()** e **logspace()**.

I'll give you a **practical example**.

To create an array with a sequence of integers from 1 to 10 type **v=1:10**

>> v=1:10

v =

1 2 3 4 5 6 7 8 9 10

In this case the step is missing because the sequence is defined only by two values START=1 and STOP=10

The end result is the same as you get by **v=1:1:10**

>> v=1:1:10

v =

1 2 3 4 5 6 7 8 9 10

To create a vector with only odd numbers you must indicate STEP=2.

Type the command **v=1:2:10**

>> v=1:2:10

v =

1 3 5 7 9

To create an array with even numbers change the start of the sequence START = 2 and indicate the increment STEP = 2.

Type **v=2:2:10**

>> v=2:2:10

v =

2 4 6 8 10

If you want to obtain a decreasing sequence, indicate an initial value higher than the final one START> STOP and a negative step STEP = -1

Type **v:10:-1:1**

>> v=10:-1:1

v =

10 9 8 7 6 5 4 3 2 1

You can also create a **sequence of real numbers**.

For example, create a sequence of real numbers from 0 to 1 with an increment STEP = 0.2 by typing the command **v:0:0.2:1**

>> v=0:0.2:1

v =

0.00000 0.20000 0.40000 0.60000 0.80000 1.00000

Now create a sequence of real numbers from -1 to 1 with a STEP increment = 0.5

Type **v=-1:0.5:1**

>> v=-1:0.5:1

v =

-1.0000 -0.5000 0.0000 0.5000 1.0000

In questo modo puoi definire e personalizzare qualsiasi sequenza numerica a seconda delle tue esigenze.

**Note**. This method is useful for creating a numeric sequence of integer or real values. It doesn't work with **complex numbers**. If you want to create a sequence of complex numbers you have to use the function **linspace()
**

The **linspace()** function allows you to create a number sequence in another way.

linspace(start, stop, n)

- The first parameter (start) is the start of the sequence.
- The second parameter (stop) is the end of the sequence.
- Parameter n is the total number of elements in the sequence.

I'll give you a practical example.

To create a sequence of real numbers from 1 to 10 made up of 5 elements, type** v=linspace(1,10,5)**

>> v=linspace(1,10,5)

v =

1.0000 3.2500 5.5000 7.7500 10.000

The linspace() function also allows you to create **complex number sequences**

To create a sequence of three complex numbers between 1 + 2i and 3 + 4i

>> v=linspace(1+2i,3+4i,3)

v =

1 + 2i 2 + 3i 3 + 4i

Finally, if you want to create a sequence of numbers in logarithmic scale use the function **logspace()**

logspace(a,b,n)

- The first parameter is the exponent of the initial extreme 10
^{a} - The second parameter is the exponent of the final extreme 10
^{b} - The third parameter n is the total number of elements in the sequence

For example, to define a sequence of 5 values included in the logarithmic scale between 10^{1} and 10^{2} (i.e. between 10 and 100) type **logspace(1,2,5)**

>> logspace(1,2,5)

ans =

10.000 17.783 31.623 56.234 100.000

Now you have a wide enough overview to define any number sequence in an array by Octave.