Numeric class in MATLAB includes signed and unsigned integers, single-precision floating-point numbers, and double-precision floating-point numbers. Generally, MATLAB stores all numeric values as double-precision floating-point. But, we can choose to store any number, or, an array of numbers, as integers or, as single precision, for better memory utilization. All numeric types allow basic array operations, such as indexing, etc.
Create Numeric Variables:
Data type | Description | Size | Typical range |
---|
double |
Double-precision
(floating-point values)
| 8 byte | +/- 1.7e +/- 308 (~15 digits) |
---|
single |
Single-precision
(floating-point values)
| 4 byte | +/- 3.4e +/- 38 (~7 digits) |
---|
int8 | 8 bit signed integer | 1 byte | -128 to 127 |
---|
int16 | 16 bit signed integer | 2 byte | -32768 to 32767 |
---|
int32 | 32 bit signed integer | 4 byte | -2147483648 to 2147483647 |
---|
int64 | Signed integer | 8 byte | -(263) to (263)-1 |
---|
uint16 | Unsigned integer | 2 byte | 0 to 65535 |
---|
uint32 | Unsigned integer | 4 byte | 0 to 4294967295 |
---|
uint64 | Unsigned integer | 8 byte | 0 to 18,446,744,073,709,551,615 |
---|
Example 1:
Matlab
% MATLAB code for Numeric Variables
gfg = single([4.4 3.9 6.9]) .* 8.4
gfg = double([4.4 3.9 6.9]) .* 8.4
gfg = int8([4.4 3.9 6.9]) .* 8.4
gfg = int16([4.4 3.9 6.9]) .* 8.4
gfg = int32([4.4 3.9 6.9]) .* 8.4
gfg = int64([4.4 3.9 6.9]) .* 8.4
Output:
gfg = 36.960 32.760 57.960
gfg = 36.960 32.760 57.960
gfg = 34 34 59
gfg = 34 34 59
gfg = 34 34 59
gfg = 34 34 59
Conversion of Numeric Types:
MATLAB provides the following functions to convert to various numeric data types:
Data type | Description | Syntax |
---|
cast | Convert data type of one variable to other | - B = cast ( A, newclass)
- B = cast (A , 'like',p)
|
---|
typecast | Convert data type without changing underlying data | |
---|
Example 2:
Matlab
% MATLAB code for conversion of Numeric Types
gfg = int8([-5 5]);
b= cast(gfg, "uint8")
Output:
Ans: b = 0 5
Example 3:
Matlab
% MATLAB code for conversion of Numeric Types
gfg = int16(-1)
X = typecast(gfg , 'uint16')
Output:
gfg = -1
X = 65535
Query Type and Value:
MATLAB provides the following data type to check various numeric value:
Data type | Description | Syntax |
---|
isinteger | Determine whether input is integer array | TF = isinteger(A) |
---|
isfloat | Determine if input is floating-point array | TF = isfloat(A) |
---|
isnumeric | Determine whether input is numeric array | TF = isnumeric(A) |
---|
isreal | Determine whether array uses complex storage | TF = isreal(A) |
---|
isfinite | Determine which array elements are finite | TF = isfinite(A) |
---|
isinf | Determine which array elements are infinite | TF = isinf(A) |
---|
isnan | Determine which array elements are NaN(Not a Number) | TF = isnan(A) |
---|
Example 4:
Matlab
% MATLAB code for Query Type and Value
gfg = isinteger(4)
g = isfloat(pi)
fg = isreal(2 + 7i)
x = isfinite(1 ./0)
y = isinf(1./0)
z = isnan(0./0)
Output:
gfg = 0
g = 1
fg = 0
x = 0
y = 1
z = 1
Numeric Value Limits:
MATLAB provides the following types to check limits of numeric value:
Type | Description | Syntax |
---|
eps | Floating-point relative accuracy | - d = eps
- d = eps(x)
- d = eps(datatype)
|
---|
flintmax |
Largest consecutive integer
in floating-point format
| - f = flintmax
- f = flintmax(precision)
|
---|
Inf | Create array of all Inf values | - X = Inf
- X = Inf(n)
- X = Inf(sz1,...,szN)
- X = Inf(sz)
- X = Inf(___,typename)
- X = Inf(___,'like',p)
|
---|
intmax | Largest value of specific integer type | - v = intmax
- v = intmax(type)
|
---|
intmin | Smallest value of specified integer type | - v = intmin
- v = intmin('classname')
|
---|
NaN | Create array of all Not a Number values | - X = NaN
- X = NaN(n)
- X = NaN(sz1,...,szN)
- X = NaN(sz)
- X =NaN(___,typename)
- X = NaN(___,'like',p)
|
---|
realmax | Largest positive floating-point number | - f = realmax
- f = realmax(precision)
|
---|
realmin | Smallest normalized floating-point number | - f = realmin
- f = realmin(precision)
|
---|
Example 5:
Matlab
% MATLAB code for Numeric Value Limits
gfg = flintmax
gfg = intmax
gfg = Inf
gfg = intmin
gfg =NaN
gfg = realmax
Output:
gfg = 9.0072e+15
gfg = 2147483647
gfg = Inf
gfg = -2147483648
gfg = NaN
gfg = 1.7977e+308