# Tips & Tricks – Indexing in Matlab

1 2 3 |
>>x=[9 21 6 8 7 3 18 -1 0 4] ans= 9 21 6 8 7 3 18 -1 0 4 |

Index with single value

1 2 3 |
>>x(2) ans = 21 |

Index with range of values [crayon-5478c5905301b54314015..." />

Consider a sample vector in Matlab.[crayon-5478c59053009865304411/]

Index with single value

[crayon-5478c59053016219913096/]Index with range of values

1 2 3 |
>> x([1 4 6]) ans = 9 8 3 |

Select a range of elements using ‘:’ operator.

Example: Select elements with index ranging from 1 to 5.

1 2 3 |
>> x(1:5) ans = 9 21 6 8 7 |

Making a new vector by swapping two halves of the vector x

1 2 3 |
>> x([6:10 1:5]) ans = 3 18 -1 0 4 9 21 6 8 7 |

To refer the last element of the matrix x use ‘end’ operator

1 2 3 |
>> x(end) ans = 4 |

You can do arithmetic on the ‘end’ operator. Selecting all elements except the last element

1 2 3 |
>> x(1:end-1) ans = 9 21 6 8 7 3 18 -1 0 |

Reversing the vector

1 2 3 |
>> x(end:-1:1) ans = 4 0 -1 18 3 7 8 6 21 9 |

Using the end operator in a range. Selecting from fifth element to the end

1 2 3 |
>> x(5:end) ans = 7 3 18 -1 0 4 |

Replacing specific elements in the array by placing the new values on the right side of the expression. Replacing the values of ‘x’ at positions [2,5,8] with new values

1 2 3 |
>> x([2 5 8])=[11 14 -6] x = 9 11 6 8 14 3 18 -6 0 4 |

Replacing specific elements with a same value. Replacing the values of ‘x’ at position [1 and 10] with 40

1 2 3 |
>> x([1 10]) = 40 x = 40 11 6 8 14 3 18 -6 0 40 |

### Two dimensional Arrays:

1 2 3 4 5 6 |
>> x=magic(4) %Create a magic array with 4x4 elements x = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 |

The two dimensional array elements are accessed with two subscript indices like x(i,j) – where ‘i’ represents row and ‘j’ represents column. Accessing the element located at second row and third column

1 2 3 |
>> x(2,3) ans = 10 |

The subscript indices can also be vectors. Selecting the elements in the first row – 4th,2nd and 1st column & third row – 4th,2nd and 1st column

1 2 3 4 |
>> x([1 3],[4 2 1]) ans = 13 2 16 12 7 9 |

The ‘:’ operator is the short form for 1:end. It is usually used to select all the elements in a specific column of row. Accessing all elements on the third row

1 2 3 |
>> x(3,:) ans = 9 7 6 12 |

Extracting the last row using the “end” operator and the “:” operator

1 2 3 |
>> x(end,:) ans = 4 14 15 1 |

### Logical Indexing:

Used to select the elements of a matrix that satisfy some criteria given by an expression

1 2 3 4 5 6 |
>>x=magic(4) %Create a magic array with 4x4 elements x = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 |

Getting all elements of the matrix whose value is above 10

1 2 3 4 5 6 7 8 |
>> x(x>10) ans = 16 11 14 15 13 12 |

Getting the row and column subscripts of those elements using the “find” function.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>>>> [i,j]=find(x>10) i = 1 2 4 4 1 3 j = 1 2 2 3 4 4 |

Sometimes, when you run your script you might encounter ‘Inf’ (IEEE arithmetic representation for positive infinity) or -Inf( representation for negative infinity) values sitting in a matrix. See the following example

1 2 3 4 5 6 |
>> k=[1 4 5 0 6 7 0 -1] x=1./k %Finding the reciprocal of each element and storing it as a vector k = 1 4 5 0 6 7 0 -1 x = 1.0000 0.2500 0.2000 Inf 0.1667 0.1429 Inf -1.0000 |

Your rest of the script may need you to fix this by replacing the Inf with 0. This can be achieved by using “isinf” function in Matlab. The “isinf” function returns a logical array which will specify whether an element is Inf (It also compares -Inf).

1 2 3 |
>> x(isinf(x))=0 x = 1.0000 0.2500 0.2000 0 0.1667 0.1429 0 -1.0000 |

There are other functions similar to “isinf” like “isspace”,”isnan” etc.., that look for the specific condition to satisfy and returns a logical array depending on the result.

### Other Resources on Matlab’s Matrix Indexing:

[1] Matlab documentation on Matrix indexing

[2]A Book detailing how MATLAB code can be written in order to maximize its effectiveness.

Richard K Johnson, “The Elements of MATLAB Style “, Cambridge University Press,ISBN-13: 978-0521732581