• Uncategorised


Subscripts Command

The element in row i and column j of A is denoted by A(i,j). For example, A(4,2) is the number in the fourth row and second column. For our magic square, A(4,2) is 15. So it is possible to compute the sum of the elements in the fourth column of A by typing

A(1,4) + A(2,4) + A(3,4) + A(4,4)

This produces

ans =

but is not the most elegant way of summing a single column.

It is also possible to refer to the elements of a matrix with a single subscript, A(k). This is the usual way of referencing row and column vectors. But it can also apply to a fully two-dimensional matrix, in which case the array is regarded as one long column vector formed from the columns of the original matrix. So, for our magic square, A(8) is another way of referring to the value 15 stored in A(4,2).

If you try to use the value of an element outside of the matrix, it is an error.

t = A(4,5)

Index exceeds matrix dimensions.
On the other hand, if you store a value in an element outside of the matrix, the size increases to accommodate the newcomer.

X = A;
X(4,5) = 17
X =
    16     3     2    13     0
     5    10    11     8     0
     9     6     7    12     0
     4    15    14     1    17

You may also like...