logical - Convert numeric values to logicals - MATLAB (original) (raw)

Main Content

Convert numeric values to logicals

Syntax

Description

L = logical([A](#bt0ojud-1-A)) converts A into an array of logical values. Any nonzero element of A is converted to logical 1 (true) and zeros are converted to logical 0 (false). Complex values and NaNs cannot be converted to logical values and result in a conversion error.

example

Examples

collapse all

Pick Odd Elements from Numeric Matrix

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 = 3x3 logical array

1 1 0 1 0 1 0 1 1

The array has logical 1 (true) values where A is odd.

Use L as a logical index to pick out the odd elements of A.

The result is a vector containing all odd elements of A.

Use the logical NOT operator, ~, on L to find the even elements of A.

Input Arguments

collapse all

A — Input array

scalar | vector | matrix | multidimensional array

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

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

Thelogical function fully supports tall arrays. For more information, see Tall Arrays.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

HDL Code Generation

Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

The logical function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a