User Tools

Site Tools


indexing_variables

This is an old revision of the document!


Indexing Variables

Using the following matrices we will discuss how to index any variable. For demonstration purposes we will use a matrix since every variable in Matlab operates like a matrix.

x = [1 2 3; 4 5 6; 7 8 9] y = [0 0 0; 1 1 1; 2 2 2]

Indexing in Matlab doesn’t start with 0, rather 1. The method for indexing the very first element in matrix x is

syntax variable_name(row,column) x(1,1); Matlabe will return the value 1 since it is the first element in matrix x.

It is possible to index a specific range in any matrix. Using the : symbol. Colon by itself will select every element in that row or column. If the colon is used between two integers this indicates a range between the two integers. Look at the following examples for better understanding.

z = x[:,2] z will consists of all the rows in column 2. [2 ; 5 ; 8]

z = x[1:2, 1:2] z will be a 2×2 matrix composed of [1 2 ; 4 5]

For more information on the colon type “help :” in the command window.

indexing_variables.1440864115.txt.gz · Last modified: 2015/08/29 10:01 by petersen