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, although this is not a requirement.

For example, let B = logical([1 0 1]) and C = logical([0 1 0]), two vectors that do match the sizes of the dimensions where they are used. Then,

equals

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, in the second element. This selects the second column. The result is the intersection of the two sets selected by B and C, that is, all the elements in the second columns of rows 1 and 3.

Or, let B = logical([1 0]) and C = logical([0 1]), two vectors that do not match the sizes of the dimensions where they are used. Then

equals

This is tricky. B, the row index, selects row 1 but does not select row 2. C, the column index, does not select column 1 but does select 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