If A is a vector ( matrix or matrix), then A[k] is naturally the kth element of A. The index doesn't have to be a scalar. If K is a matrix itself, then A[K] would be a matrix of the same dimensions. For example
>> A = [2, 3, 5, 7, 11, 13, 17, 19, 23]; >> A[3] 5 >> K = [1, 3, 5, 7]; >> A[K] 2 5 11 17 >> J = [1, 3; 5, 7] >> A[J] 2 5 11 17
Even if A is not a vector, it is still possible to use a single index expression to A. If matrix A has c columns, and , then A[k] refers to the element by of A at -th row and -th column. In other words, A[k] is the -th element of the row vector obtained by horizontally joining all the rows of A.
>> C = rand(3,5) 0.802 0.716 0.262 0.752 0.925 0.65 0.489 0.327 0.859 0.655 0.396 0.329 0.941 0.854 0.857 >> C[7] 0.489 >> C[10] 0.655
The indexing expression can be a matrix itself. In this case a matrix with the same size as that of the indexing matrix is created, whose elements are the elements of the indexed matrix at the positions specified by the elements of the indexing matrix.
A = rand(7) K = 1 : 2 : 7 A[k] A = rand(5, 3) K = [1, 3; 5, 7] A[k]