Using the C++ Math Library    

Using Two Logical Vectors as Indices

Two vectors can be logical indices into an M-by-N matrix A. The size of a logical vector index often matches the size of the dimension it indexes though this is not a requirement.

For example, let B = [1 0 1] and C = [0 1 0], two 1-by-3 logical vectors. Then, A(B, C) is

B, the row index vector, has nonzero entries in the first and third elements. This selects the first and third rows. C, the column index vector, has only one nonzero entry, the second element. This selects the second column. The result is the intersection of the two sets selected by B and C, all the elements in the second columns of rows 1 and 3.

Or, if B = [1 0] and C = [0 1], then A(B,C) equals:

This is tricky. B, the row index, selects row 1. C, the column index, selects column 2. There is only one element in array A in both row 1 and column 2, the element 4.


 Using a Logical Matrix As a One-Dimensional Index Using One colon() Index and One Logical Vector as Indices