Programming and Data Types

Subscripting and Indexing

Subscripting

This section explains how to use subscripting to access and assign to elements of a MATLAB matrix. It covers the following:

Accessing Single Elements of a Matrix

The element in row `i` and column `j` of `A` is denoted by `A(i,j)`. For example, suppose `A = magic(4)`, Then `A(4,2)` is the number in the fourth row and second column. For our magic square, `A(4,2)` is `14`.

• ```A = magic(4)
A =
16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1

A(4,2)
ans =
14
```

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 `14` stored in `A(4,2)`.

• ```A(8)
ans =
14
```

Accessing Multiple Elements of a Matrix

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)
```

You can reduce the size of this expression using the colon operator. Subscript expressions involving colons refer to portions of a matrix.

• ```A(1:m,n)
```

refers to the elements in rows `1` through `m` of column `n` of the `A` matrix. Using this notation, you can compute the sum of the fourth column of `A` more succinctly.

• ``````sum(A(1:4,4))
``````

The colon by itself refers to all the elements in a row or column of a matrix. The keyword `end` refers to the last row or column. Using the following syntax, you can compute this same column sum without having to specify row and column numbers.

• ```sum(A(:,end))
ans =
34
```

By adding an additional colon operator, you can refer to nonconsecutive elements in a matrix. The `m:3:n` in this expression means to make the assignment to every third element in the matrix.

• ```B = A;

B(1:3:end) = -10
B =
-10     2     3   -10
5    11   -10     8
9   -10     6    12
-10    14    15   -10
```

You can repeatedly access an array element using the `ones` function. To create a new 2-by-6 matrix out of the the 9th element of `A`,

• ```B = A(9*ones(2,6))
B =
3     3     3     3     3     3
3     3     3     3     3     3
```

Expanding the Size of a Matrix

If you try to access an element outside of the matrix, it is an error

• ```B = A(4,5)
Index exceeds matrix dimensions
```

However, if you store a value in an element outside of the matrix, the size of the matrix increases to accommodate the new element.

• ```B = A;

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

Similarly, you can expand a matrix by assigning to a series of matrix elements.

• ```B(2:5,5:6) = 5
B =
16     2     3    13     0     0
5    11    10     8     5     5
9     7     6    12     5     5
4    14    15     1     5     5
0     0     0     0     5     5
```

Deleting Rows and Columns

You can delete rows and columns from a matrix using just a pair of square brackets. Start with

• ```X = A;
```

Then, to delete the second column of `X`, use

• ```X(:,2) = []
```

This changes `X` to

• ```X =
16    3   13
5   10    8
9    6   12
4   15    1
```

If you delete a single element from a matrix, the result isn't a matrix anymore. So expressions like

• ```X(1`,`2) = []
```

result in an error. However, using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector. So

• ```X(2:2:10) = []
```

results in

• ```X =
16     9     3     6    13    12     1
```

Concatenating Matrices

Concatenation is the process of joining small matrices together to make bigger ones. In fact, you made your first matrix by concatenating its individual elements. The pair of square brackets, `[]`, is the concatenation operator. For an example, start with the 4-by-4 magic square, `A`, and form

• ```B = [A A+32;A+48 A+16]
```

The result is an 8-by-8 matrix, obtained by joining the four submatrices.

• ```B =
16     2     3    13    48    34    35    45
5    11    10     8    37    43    42    40
9     7     6    12    41    39    38    44
4    14    15     1    36    46    47    33
64    50    51    61    32    18    19    29
53    59    58    56    21    27    26    24
57    55    54    60    25    23    22    28
52    62    63    49    20    30    31    17
```

This matrix is half way to being another magic square. Its elements are a rearrangement of the integers `1:64`. Its column sums are the correct value for an 8-by-8 magic square.

• ```sum(B)
ans =
260   260   260   260   260   260   260   260
```

But, its row sums, `sum(B')'`, are not all the same. Further manipulation is necessary to make this a valid 8-by-8 magic square.