## Indices

Often it is desirable to work with parts or separate elements of matrices. Hereto, each element of a matrix can be accessed by means of indices. The command for this has the general form >> A(m,n). Here is the matrix of which the element with row index and column index is being specified. If is a vector, one index suffices. For the matrix in exercise 4.1, >> A(3,2) gives the value of this element, namely . Among others, this enables you to change one single element in the matrix. The command:

>> A(3,2) = 3

changes the original value -2 of the element on the third row and second column of into the new value 3. All other elements of remain the same. Instead of indicating one element of , you can also indicate more elements of at the same time. This is done by giving a vector of indices instead of one single index.

>> A(3,[2 4])

gives the elements and from arranged as in , i.e., as a 1 x 2 matrix. Hence, the command

>> B=A([1 2 3],[2 4])

gives the 3 x 2 matrix consisting of the elements of in the first three rows and columns two and four of . For example, applying the previously mentioned command on a matrix

results in

In view of the remarks from paragraph 4.2.4, this could also have been accomplished with the command

>> A(1:3,[2 4])

When only :' is used, all rows or columns are meant. This can appear in the following forms:

• A(:,j) is the column of .
• A(i,:) is the row of .
• A(:,:) is the same as .
This possibility is especially useful if you want to perform operations with whole rows or columns at once. For example, for our original matrix , the command:

>> A(2,:)-7*A(1,:)

gives the row vector that results from subtracting seven times the first row of from the second row of . With the command:

>> A(2,:) = A(2,:)-7*A(1,:)`

the same result is calculated and assigned to the second row of . Thus, this command has changed the matrix .

Esteur 2010-03-22