## Introduction

In this tutorial we learn how to index a vector in MATLAB. Properly indexing vectors is necessary when working with them in MATLAB. If you want to use MATLAB effectively then understanding this topic can help you a lot in learning to properly access modify or use values stored in a vector.

## Defining a Vector that will be indexed

Lets start by defining a vector of length 10 and call it “a”. We are going to use this vector throughout this tutorial to understand various ways of indexing a vector. We can write the following command in the command window to define “a”

```
>> a = [10 20 30 40 50 60 70 80 90 100]
a =
10 20 30 40 50 60 70 80 90 100
```

## How to index a vector in MATLAB

There are 4 main methods for indexing vectors in MATLAB. Lets look at each one in detail

### Indexing a vector using a single index

First way of indexing a vector is by using a single index or element number. Using this method you can access or modify (depending on how the command is written) a single element of vector “a” at a time. The syntax for this method is

`vector_name(index)`

Indexing in MATLAB starts from 1. *index *can be any value from 1 to the length of vector. Lets look at an example

```
>> a(5)
ans =
50
```

a(5) returns the 5th element of vector “a” which is 50 in our case. Lets try another example

```
>> a(end)
ans =
100
```

a(end) returns the last element of vector “a”. *end* always equals the length (i.e. last index) of the vector. a(end-1) should return the second last element. We can easily verify this

```
>> a(end-1)
ans =
90
```

*index *cannot be 0 or greater than length of the vector. Using a(0) or a(12) result in an error since they are not valid indices.

```
>> a(0)
Subscript indices must either be real positive integers or logicals.
```

```
>> a(12)
Index exceeds matrix dimensions.
```

You can change the value of one of the elements of vector “a” by assigning it a new value. Lets look at an example

```
>> a(1) = 50
a =
50 20 30 40 50 60 70 80 90 100
```

a(1) is now 50.

### Indexing a vector using a vector of indices

Lets redefine vector “a” on which we are going to do indexing on.

```
>> a = [10 20 30 40 50 60 70 80 90 100]
a =
10 20 30 40 50 60 70 80 90 100
```

You can access more than one element of a vector by giving a *vector of indices*. MATLAB returns a vector of same length as the input vector. Moreover, the order of elements in the returned vector is according to the order of indices in the input vector.

`vector_name(vector_of_indices)`

Lets see an example

```
>> a([5 1 end-1])
ans =
50 10 90
```

All the indices in the input vector must be valid indices otherwise MATLAB will give an error.

```
>> a([5 0 10])
Subscript indices must either be real positive integers or logicals.
```

```
>> a([5 15 10])
Index exceeds matrix dimensions.
```

You can use this indexing method to modify the selected elements of a vector. Lets look at an example where all the indexed elements are replaced with a single value

```
>> a([4 3 2 1]) = 20
a =
20 20 20 20 50 60 70 80 90 100
```

If you want to assign separate values to each element then you can provide a vector of new values. The vector must be the same length as the vector used for indexing. Each element at the index specified in the indexing vector will be replaced with corresponding entry from the vector of new values.

```
>> a([3 2 1 4]) = [30 20 10 40]
a =
10 20 30 40 50 60 70 80 90 100
```

MATLAB returns an error if the vector length is not the same as “shape” of left side.

```
>> a([3 2 1 4]) = [30 20]
In an assignment A(:) = B, the number of elements in A and B must be the same.
```

### Indexing a vector using a range

The third method for indexing vector is by using a range of indices that is defined using : (colon). The syntax is

`vector_name(range_low:increment:range_high)`

*range_low *and *range_high *are the start and end of the range (of indices).* *In this case MATLAB generates a vector from *range_low *to *range_high* with the given *increment *and then uses it to index the vector. *increment *is optional and can be omitted. The syntax for such a case is

`vector_name(range_low:range_high)`

In this case MATLAB assumes increment of 1 and generates a vector of length *range_high*–*range_low*–*1* consisting of numbers from *range_low* to *range_high*. Lets try a few examples

```
>> a(1:6)
ans =
10 20 30 40 50 60
```

a(1:6) returns a vector of first six elements of vector “a”. The increment is 1 (default) since we didn’t specify it during indexing.

Writing a(0:6) or a(1:15) return an error because our range has invalid indices.

```
>> a(0:6)
Subscript indices must either be real positive integers or logicals.
```

```
>> a(1:15)
Index exceeds matrix dimensions.
```

If odd elements of “a” are required we can write.

```
>> a(1:2:10)
ans =
10 30 50 70 90
```

100 is not included in the output even though we gave 10 as *range_high.* This is due to starting from 1 and using increment of 2. Index 10 is not included in our range. Increment can also be negative. We can get the elements of “a” in reverse order by using the following command

```
>> a(end:-1:1)
ans =
100 90 80 70 60 50 40 30 20 10
```

### Logical indexing

In logical indexing, instead of using an index, vector or range, we provide a logical expression. As an example, to get all the elements of “a” that are less than 50, we can write

```
>> a(a<50)
ans =
10 20 30 40
```

To replace all the elements of “a” that are greater or equal to 70, we can write

```
>> a(a>=70) = 10
a =
10 20 30 40 50 60 10 10 10 10
```

I have done a video version of the same tutorial in Urdu / Hindi on Youtube. Please subscribe to my Youtube channel.

## 1 thought on “How to Index a Vector in MATLAB”