| MATLAB Functions | Help Desk |
logical
Convert numeric values to logical
K = logical(A)
K = logical(A)
returns an array that can be used for logical indexing or logical tests.The array K is the same size as A and is displayed using 1 where corresponding elements of A are nonzero, and 0 where corresponding elements of A are zero.
Logical arrays are also created by the relational operators (==,<,>,~, etc.) and functions like any, all, isnan, isinf, and isfinite.
Given A = [1 2 3; 4 5 6; 7 8 9], the statement B = logical(eye(3)) returns a logical array
B = 1 0 0 0 1 0 0 0 1which can be used in logical indexing that returns
A's diagonal elements:
A(B)
ans =
1
5
9
However, attempting to index into A using the numeric array eye(3) results in:
A(eye(3))
??? Index into matrix is negative or zero.