# How to get a value from an array in Matlab and Octave

To read the value of an element from a vector v in Matlab and Octave, type the name of the array and the index position of the element in round brackets.

**v(n)**

The term n is an integer indicating the position of the element in the vector index.

If n = "end" Matlab reads the last index position of the vector (last element).

**Note**. Unlike many programming languages, vector indexing starts from 1 (not 0) in Matlab and Octave. Thus, the first element of the vector is v (1). The second element of the vector is v (2). Typing v (0) causes an error in Matlab and Octave.

## Example

Define a vector with 5 elements

>> v=['a';'b';'c';'d';'e']

To get the first element of the vector, type **v(1)**

>> v(1)

ans = a

To get the second element, type **v(2)**

>> v(2)

ans = b

To get the third element, type **v(3)**

>> v(3)

ans = c

To get the last element, type **v(5)**

>> v(5)

ans = e

Alternatively, to access the last element without specifying the position, use the word "end" and type **v(end)**

>> v(end)

ans = e

In this case the software automatically finds the last position in the index of the vector, before reading and returning the value of the element.

**Report us an error or send a suggestion to improve this page**