true - Logical 1 (true) - MATLAB (original) (raw)

Syntax

Description

true is shorthand for the logical value 1.

example

[T](#bt7dazv-1-T) = true([n](#bt7dazv-1-n)) is an n-by-n matrix of logical ones.

example

[T](#bt7dazv-1-T) = true([sz](#bt7dazv-1-sz)) is an array of logical ones where the size vector, sz, defines size(T). For example, true([2 3]) returns a 2-by-3 array of logical ones.

example

[T](#bt7dazv-1-T) = true([sz1,...,szN](#bt7dazv-1-sz1szN)) is a sz1-by-...-by-szN array of logical ones where sz1,...,szN indicates the size of each dimension. For example, true(2,3) returns a 2-by-3 array of logical ones.

example

[T](#bt7dazv-1-T) = true(___,like=[p](#bt7dazv-1-p)) returns an array of logical ones of the same sparsity as the logical variablep using any of the previous size syntaxes.

example

Examples

collapse all

Use true to generate a 3-by-3 square matrix of logical ones.

A = 3×3 logical array

1 1 1 1 1 1 1 1 1

The result is of class logical.

Use true to generate a 3-by-2-by-2 matrix of logical ones.

ans = 3×2×2 logical array ans(:,:,1) =

1 1 1 1 1 1

ans(:,:,2) =

1 1 1 1 1 1

Alternatively, you can use a size vector to specify the size of the matrix.

ans = 3×2×2 logical array ans(:,:,1) =

1 1 1 1 1 1

ans(:,:,2) =

1 1 1 1 1 1

Note that specifying multiple vector inputs returns an error.

true along with false can be used to execute logic statements.

Test the logical statement

~(A and B) = (~A) or (~B)

for A = true and B = false.

(true & false) == (true) | (~false)

The result is logical 1 (true), since the logical statements on both sides of the equation are equivalent. This logical statement is an instance of De Morgan's Law.

Generate a logical array of the same sparsity as the selected array.

A = logical(sparse(5,3)); whos A

Name Size Bytes Class Attributes

A 5x3 41 logical sparse

T = true(4,like=A); whos T

Name Size Bytes Class Attributes

T 4x4 184 logical sparse

The output array T has the same sparse attribute and data-type as the specified array A.

Input Arguments

collapse all

Size of square matrix, specified as an integer. n sets the output array size to n-by-n. For example, true(3) returns a 3-by-3 array of logical ones.

Data Types: int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size vector, specified as a row vector of integers. For example, true([2 3]) returns a 2-by-3 array of logical ones.

Data Types: int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size inputs, specified by a comma-separated list of integers. For example, true(2,3) returns a 2-by-3 array of logical ones.

Data Types: int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Prototype, specified as a logical variable.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

Output Arguments

collapse all

Output of logical ones, returned as a scalar, vector, matrix, or N-D array.

Data Types: logical

Tips

Extended Capabilities

expand all

Usage notes and limitations:

Usage notes and limitations:

The true 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).

Version History

Introduced before R2006a