Programming and Data Types |
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
.
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)
.
Accessing Multiple Elements of a Matrix
It is possible to compute the sum of the elements in the fourth column of A
by typing
You can reduce the size of this expression using the colon operator. Subscript expressions involving colons refer to portions of a matrix.
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.
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.
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.
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
,
Expanding the Size of a Matrix
If you try to access an element outside of the matrix, it is an error
However, if you store a value in an element outside of the matrix, the size of the matrix increases to accommodate the new element.
Similarly, you can expand a matrix by assigning to a series of matrix elements.
You can delete rows and columns from a matrix using just a pair of square brackets. Start with
Then, to delete the second column of X
, use
If you delete a single element from a matrix, the result isn't a matrix anymore. So expressions like
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
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
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.
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.
Obtaining User Input | Advanced Indexing |