single - Single-precision arrays - MATLAB (original) (raw)
Main Content
Description
Single-precision variables in MATLABĀ® are stored as 4-byte (32-bit) floating-point values of data type (class)single
. For example:
Name Size Bytes Class Attributes
y 1x1 4 single
For more information on floating-point values, see Floating-Point Numbers.
Creation
If you have an array of a different type, such as double
orint8
, then you can convert that array to a single precision array by using the single
function.
Syntax
Description
Y = single([X](#d126e1670592))
converts the values inX
to single precision.
Input Arguments
Input array, specified as a scalar, vector, matrix, or multidimensional array.
Data Types: double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
Examples
Convert a double-precision variable to single precision with the single
function.
x = 100; xtype = class(x)
Extended Capabilities
Thesingle
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- When using single-precision integers with the colon operator, if either of the end points have a value that is greater in absolute value than
flintmax('single')
in your MATLAB code, then the generated code might produce different values as compared to the MATLAB code. For example:
function z = mismatch_values
a = single(1);
b = flintmax('single') + 2;
d = single(9);
z = a:d:b;
end
Thez
values that are calculated by the generated code and MATLAB code are different due to the different rounding methods used by the generated code and MATLAB code.
The single
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