Skip to Main Content Skip to Search
Product Documentation

logical - Convert numeric values to logical

Syntax

K = logical(A)

Description

K = logical(A) returns an array that can be used for logical indexing or logical tests.

A(B), where B is a logical array that is the same size as A, returns the values of A at the indices where the real part of B is nonzero.

A(B), where B is a logical array that is smaller than A, returns the values of column vector A(:) at the indices where the real part of column vector B(:) is nonzero.

Tips

Most arithmetic operations remove the logicalness from an array. For example, adding zero to a logical array removes its logical characteristic. A = +A is the easiest way to convert a logical array, A, to a numeric double array.

Logical arrays are also created by the relational operators (==,<,>,~, etc.) and functions like any, all, isnan, isinf, and isfinite.

Examples

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   1

which 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))
??? Subscript indices must either be real positive integers or 
logicals.

See Also

false | islogical | Logical Operators: Elementwise | Logical Operators: Short-circuit | true

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS