true
Logical 1 (true)
Description
true
is shorthand for the logical value 1
.
is
a T
= true(sz1,...,szN
)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.
Examples
Generate Square Matrix of Logical Ones
Use true
to generate a 3-by-3 square matrix of logical ones.
A = true(3)
A = 3x3 logical array
1 1 1
1 1 1
1 1 1
class(A)
ans = 'logical'
The result is of class logical
.
Generate Array of Logical Ones with Arbitrary Dimensions
Use true
to generate a 3-by-2-by-2 matrix of logical ones.
true(3,2,2)
ans = 3x2x2 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.
true([3,2,2])
ans = 3x2x2 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.
Execute Logic Statement
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)
ans = logical
1
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 Logical Array of Selected Sparsity
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
n
— Size of square matrix
integer
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.
If
n
is0
, thenT
is an empty matrix.If
n
is negative, then it is treated as0
.
Data Types: int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
sz
— Size vector
row vector of integers
Size vector, specified as a row vector of integers. For example, true([2
3])
returns a 2-by-3 array of logical ones.
If the size of any dimension is
0
, thenT
is an empty array.If the size of any dimension is negative, then it is treated as
0
.If any trailing dimensions greater than
2
have a size of1
, then the output,T
, does not include those dimensions.
Data Types: int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
sz1,...,szN
— Size inputs
comma-separated list of integers
Size inputs, specified by a comma-separated list of integers.
For example, true(2,3)
returns a 2-by-3 array of
logical ones.
If the size of any dimension is
0
, thenT
is an empty array.If the size of any dimension is negative, then it is treated as
0
.If any trailing dimensions greater than
2
have a size of1
, then the output,T
, does not include those dimensions.
Data Types: int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
p
— Prototype
logical variable
Prototype, specified as a logical variable.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Complex Number Support: Yes
Output Arguments
T
— Output of logical ones
scalar | vector | matrix | N-D array
Output of logical ones, returned as a scalar, vector, matrix, or N-D array.
Data Types: logical
Tips
true(n)
is much faster and more memory efficient thanlogical(true(n))
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Dimensions must be real, nonnegative, integers.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Dimensions must be real, nonnegative, integers.
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 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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
For additional
codistributed
syntaxes, seetrue (codistributed)
(Parallel Computing Toolbox).
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)