Main Content

logical

将数字值转换为逻辑值

Description

example

l =逻辑(A)convertsAinto an array of logical values. Any nonzero element ofAis converted to logical1(true) and zeros are converted to logical0(false). Complex values and NaNs cannot be converted to logical values and result in a conversion error.

Examples

collapse all

Pick out the odd-numbered elements of a numeric matrix.

Create a numeric matrix.

A = [1 -3 2;5 4 7;-8 1 3];

Find the modulus,mod(A,2), and convert it to a logical array for indexing.

l =逻辑(mod(A,2))
L =3x3 logical array1 1 0 1 0 1 0 1 1

The array has logical1(true) values whereAis odd.

UseLas a logical index to pick out the odd elements ofA.

A(L)
ans =6×11 5 -3 1 7 3

The result is a vector containing all odd elements ofA.

Use the logical NOT operator,~, onLto find the even elements ofA.

A(~L)
ans =3×1-8 4 2

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char

Tips

  • Most arithmetic operations involving logical arrays return double values. For example, adding zero to a logical array returns a double array.

  • Logical arrays also are created by the relational operators (==,<,>,~等)和功能像任何,all,isnan,isinf, andisfinite.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
使用GPU CODER™为NVIDIA®GPU生成CUDA®代码。

HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

版本历史

Introduced before R2006a