Indexing arrays
Indexing in programming languages is a mechanism for accessing an element of a data array by referring to an array and by using one or more expressions, the values of which determine the position of the array component. The index is the number of an element of the set that points to a specific element of the array. This example will demonstrate how to work with indexes.
Let's create a 4-by-4 matrix, the intexes of which we will refer to:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
There are two ways to refer to a specific element in an array. The most common way is to specify row and column indexes.:
A[2, 2]
It is less common, but sometimes useful, to use a single index that runs down each column in order.:
println(A[1], " ", A[2], " ", A[3], " ", A[4], " ", A[5])
Using a single index to refer to a specific element in an array is called linear indexing.
To refer to multiple array elements, use the colon operator, which allows you to specify a range of the form start:end. For example, list the elements in the first three rows and the second column of the array.:
A[1:3, 2]
A colon, without a start or end value, indicates all the elements in this dimension. For example, let's display all the columns in the third row of the table.:
A[3, :]
The colon operator also allows you to create a vector of values with equal intervals using the more general form start:step:end:
B = collect(0:10:100)
If you omit the step, as in start:end, Julia will use the default step value of 1.:
B = collect(0:100)