Y
= cos(
X
)
returns
the cosine for each element of
X
. The
cos
function operates element-wise on arrays. The function accepts both real and complex
inputs.
For real values of
X
,
cos(X)
returns real values in the interval [-1, 1].
For complex values of
X
,
cos(X)
returns complex values.
The cosine of an angle, α, defined with reference to a right triangle
is
The cosine of a complex argument, α, is
Tips
To compute
cos(X*pi)
accurately, without using
pi
as a floating-point approximation of π, you can use
the
cospi
function instead. For example,
cospi(m/2)
is exactly zero for odd integers
m
and
cospi(n)
is +1 or –1 for
integers
n
.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
cos
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™.
Thread-Based Environment
Run code in the background using MATLAB®
backgroundPool
or accelerate code with Parallel Computing Toolbox™
ThreadPool
.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The
cos
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™.
R2023a:
Perform calculations directly on tables and timetables
The
cos
function can calculate on all variables within a table or
timetable without indexing to access those variables. All variables must have data types
that support the calculation. For more information, see
Direct Calculations on Tables and Timetables
.