Integer division with rounding option
collapse all in page
Syntax
C = idivide(A,B)
C = idivide(A,B,opt)
Description
example
C = idivide(A,B)
divides each element of A
by the corresponding element of B
, rounded to the nearest integers toward zero. A
and B
must contain real numbers and at least one of them must belong to an integer class.
If
A
andB
are arrays, then they must belong to the same integer class and have sizes that are compatible.If
A
orB
is a scalar double, then the other input must be an integer class, but notint64
oruint64
. Theidivide
function then returnsC
as the same integer class.
example
C = idivide(A,B,opt)
specifies an alternative rounding option: 'fix'
, 'floor'
, 'ceil'
, or 'round'
. For example, idivide(A,B,'ceil')
rounds the quotients to the nearest integers toward positive infinity. The default rounding option is 'fix'
.
Examples
collapse all
Integer Division
Open Live Script
Create an integer array A
and an integer scalar B
. Divide each element of A
by B
, rounded to the nearest integers toward zero.
A = int16([-7 -4 7 11]);B = int16(10);C = idivide(A,B)
C = 1x4 int16 row vector 0 0 0 1
Divide Two Integer Arrays
Open Live Script
Create two integer arrays, A
and B
. Divide each element of A
by the corresponding element of B
, rounded to the nearest integers toward zero.
A = int64([-2 3]);B = int64([3 5]);C = idivide(A,B)
C = 1x2 int64 row vector 0 0
Rounding Options for Integer Division
Open Live Script
Create a scalar double A
and an integer array B
. Divide A
by each element of B
with the default rounding option 'fix'
.
A = 2.0;B = int32([-3 3 4]);C = idivide(A,B)
C = 1x3 int32 row vector 0 0 0
Compare the results with other rounding options.
C = idivide(A,B,'floor')
C = 1x3 int32 row vector -1 0 0
C = idivide(A,B,'ceil')
C = 1x3 int32 row vector 0 1 1
C = idivide(A,B,'round')
C = 1x3 int32 row vector -1 1 1
Input Arguments
collapse all
A
— Numerator
scalar | vector | matrix | multidimensional array
Numerator, specified as a scalar, vector, matrix, or multidimensional array. Integer inputs A
and B
must be either the same size or have sizes that are compatible. For example, A
is an M
-by-N
matrix, and B
is a scalar or 1
-by-N
row vector. For more information, see Compatible Array Sizes for Basic Operations.
If A
is a scalar double, then B
must be an integer class, but not int64
or uint64
.
Data Types: double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
B
— Denominator
scalar | vector | matrix | multidimensional array
Denominator, specified as a scalar, vector, matrix, or multidimensional array. Integer inputs A
and B
must be either the same size or have sizes that are compatible. For example, A
is an M
-by-N
matrix, and B
is a scalar or 1
-by-N
row vector. For more information, see Compatible Array Sizes for Basic Operations.
If B
is a scalar double, then A
must be an integer class, but not int64
or uint64
.
Data Types: double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
opt
— Rounding option
'fix'
(default) | 'floor'
| 'ceil'
| 'round'
Rounding option, specified as 'fix'
, 'floor'
, 'ceil'
, or 'round'
.
'fix'
rounds to the nearest integers toward zero, which is equivalent to removing any digits after the decimal point.'floor'
rounds to the nearest integers toward negative infinity.'ceil'
rounds to the nearest integer toward positive infinity.'round'
rounds to the nearest integers. If an element has a fractional part of exactly 0.5, then it rounds away from zero to the integer with larger magnitude.
Output Arguments
collapse all
C
— Integer solution
scalar | vector | matrix | multidimensional array
Integer solution, returned as a scalar, vector, matrix, or multidimensional array. If either A
or B
is an integer data type, then C
is the same integer data type.
Data Types: int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Tips
The function
idivide(A,B,'round')
is the same asA./B
andB.\A
for integer data types. The argumentopt
provides the rounding options forA./B
andB.\A
when dividing integers.MATLAB® does not support complex integer division.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. Formore information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
For efficient generated code, MATLAB rules for divide by zero are supported only for the
'round'
option.Code generation does not support sparse matrix inputs for this function.
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™.
This function fully supports GPU arrays. 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™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2006a
See Also
ceil | floor | fix | round | rdivide | ldivide
Topics
- Integers
- Floating-Point Numbers
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office