Main Content

idivide

Integer division with rounding option

Description

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 and B are arrays, then they must belong to the same integer class and have sizes that are compatible.

  • If A or B is a scalar double, then the other input must be an integer class, but not int64 or uint64. The idivide function then returns C 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'.

example

Examples

collapse all

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

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

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

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

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

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

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 as A./B and B.\A for integer data types. The argument opt provides the rounding options for A./B and B.\A when dividing integers.

  • MATLAB® does not support complex integer division.

Extended Capabilities

Version History

Introduced in R2006a