% MatlabAdvIndexing.txt % % Advanced Indexing % % How to access entries in arrays in Matlab¨. % Covers three types of indexing: % 1) Using arrays of row and col indices % 2) Using linear indexing for two-dimensional arrays % 3) Using logical values for indexing %------------------------------------------------------------------------------- % Using arrays of row and col indices. %------------------------------------------------------------------------------- >> A = magic(3) % Create a magic square so we have something to work with. A = 8 1 6 3 5 7 4 9 2 % Suppose we wish to create a new matrix, B, with some values extracted from A. >> B = A([2, 1], [1, 3, 1, 2]) % Seems like we are asking for 4 rows, an error? ans = 3 7 3 5 8 6 8 1 % Here's what Matlab¨ did... % Matlab¨ created an array of indexes into A where the [2, 1] array gave the % row numbers in A, and the [1, 3, 1, 2] array gave the col numbers in A. % The following diagram shows how Matlab¨ generates indices: % cols: % cols: 1 3 1 2 % rows: 2 (2,1) (2,3) (2,1) (2,2) % row 2 cols 1, 3, 1, and 2 % 1 (1,1) (1,3) (1,1) (1,2) % row 1 cols 1, 3, 1, and 2 % % The B matrix consists of the values of A with the above indices: % B = % cols: 1 3 1 2 % rows: 2 A(2,1)=3 A(2,3)=7 A(2,1)=3 A(2,2)=5 % 1 A(1,1)=8 A(1,3)=6 A(1,1)=8 A(1,2)=1 % We can figure out what result we get by creating the above chart where % we write the rows array vertically and the cols array horizontally. % Note how the B matrix can be any size we like, and we can shuffle entries in A % to create B. %------------------------------------------------------------------------------- % Using linear indexing for two-dimensional arrays %------------------------------------------------------------------------------- % One might think the following command would produce an error, but Matlab¨ % accepts the request. >> A(6) % Is this row 6? No. Is this col 6? No. This is the 6th entry in A. ans = 9 % Matlab¨ stores 2-dimensional arrays internally in linear lists. Our A matrix % is stored in a linear fashion as the 1st col, then the 2nd col below that, % then the 3rd col below that, and so forth. Here is our A matrix and the % linear view of A. >> A = magic(3) % Create a magic square so we have something to work with. A = 8 1 6 3 5 7 4 9 2 % The linear view of A: A = 8 % Start of col 1 A(1) 3 A(2) 4 A(3) 1 % Start of col 2 A(4) 5 A(5) 9 A(6) 6 % Start of col 3 A(7) 7 A(8) 2 A(9) % Note: memory in the computer is organized as one long column, so the linear % view of A corresponds to how the contents of A are actually stored in the % computer. % If we only use one index for a 2-dimensional array, Matlab¨ will use the % linear view of the array. This "one index" may still be an array such as, % e.g., [1, 7], but there will not be a second array specifying columns. That % second array would follow the comma inside the ( )'s surrounding the index. >> A([1, 7]) % If the linear indexing array is horizontal, the answer is % horizontal ans = 8 6 >> A([1, 7]') % If the linear indexing array is vertical, the answer is % vertical ans = 8 6 %------------------------------------------------------------------------------- % Using an array for linear indexing of two-dimensional arrays %------------------------------------------------------------------------------- % What if we use an array for linear indexing? >> A(A) ans = 7 8 9 4 5 6 1 2 3 % Our array is the same shape as the indexing array (the A inside the ( )'s) % What we get is the following: A(8) A(1) A(6) A(3) A(5) A(7) A(4) A(9) A(2) % This is the same as: A(A(1,1)) A(A(1,2)) A(A(1,3)) A(A(2,1)) A(A(2,2)) A(A(2,3)) A(A(3,1)) A(A(3,2)) A(A(3,3)) % Note that we do not have to have a square matrix for the indexing. We could % do the following, for example: A([1, 2; 3, 5]) ans = 7 4 1 5 % This is the same as: A(1) A(2) A(3) A(5) % Another example: A(ones(2,4)) ans = 7 7 7 7 7 7 7 7 % This is the same as: A(1) A(1) A(1) A(1) A(1) A(1) A(1) A(1) %------------------------------------------------------------------------------- % Using logical values for indexing. pp. 2-26 to 2-27 %------------------------------------------------------------------------------- >> A = magic(3) % Create a magic square so we have something to work with. A = 8 1 6 3 5 7 4 9 2 % Comparisons create arrays of 1's and 0's representing True and False for each % entry of a matrix. Here, a 1 appears where the entry in A is greater than 4, % and a 0 appears where the entry in A is not greater than 4. >> D = A>4 D = 1 0 1 0 1 1 0 1 0 % Note: the values in D are logical values, not integer values 0 or 1. % If we make a similar array (call it D) of 0's and 1's, the next step % we do here will not work unless we first use logical(D). % D has the same shape as A. The 1's mark the spots in A where values are % greater than 4. >> A(D) % Now extract the values in A that are greater than 4 ans = 8 5 9 6 7 % Note that our answer is a column of values from A that are greater than 4. % The order of the numbers is the order in which the values occur in A when A is % viewed as a linear array. (Col 1 then col 2, etc.) % Suppose we want to create a matrix that is A with entries not greater than 4 % zeroed out. The following does the trick. Note that Matlab¨ converts the % logical matrix D into a numerical matrix of integer 0's and 1's before the % matrix multiplication is performed. >> A.*D ans = 8 0 6 0 5 7 0 9 0 %------------------------------------------------------------------------------- % Using find( ) for extracting values from two-dimensional arrays. pp. 2-27 %------------------------------------------------------------------------------- >> A = magic(3) % Create a magic square so we have something to work with. A = 8 1 6 3 5 7 4 9 2 >> find(A>4) % Finds the indices of A (treated as linear array) where A > 4 ans = 1 5 6 7 8 % Note: these are not the values that are greater than 4 but rather the indices % or locations in A where those values occur. That is why we have a 1 in % the answer. A(1) = 8 is the first entry in A, and 8 > 4. % This list of indices will always be in order from smalles index to largest. % To retrieve the entries of A that exceed 4, we use the results of the find( ) % as the indices for A. >> A(find(A>4)) ans = 8 5 9 6 7 % These values appear in the order in which they occur in A (treated as linear % array). % Note: the logical expression in the find expression may be any expression that % returns a logical value for each entry in A. % Note: when testing values whether entries in A are equal to some value, exact % equality may not occur. It is best to use a bracketting test such as the % following that tests for values in A that are equal to, for example, 3. >> find(abs(A - 3) < 10 * eps) ans = 2 % This means A(2) == 3.