Matlab

How To Check if a Number Is an Integer in Matlab

Checking whether a number is an integer is a common task in MATLAB. An integer is a whole number without any fractional or decimal part. In this post, we’ll look at a few different MATLAB techniques for determining whether a number is an integer.

Method 1: Using the isinteger() Function

MATLAB provides the built-in function isinteger() to determine if a value is of integer type. It returns a logical value of true if the input is an integer, and false otherwise, here is an illustration of the use of isinteger() function that checks if the number is an integer:

number = 10;
isInteger = isinteger(int8(number));
 
% Display the result
disp(isInteger);

 

In the code, the isinteger() function is used to check if the input value is of the integer data type. To ensure that the number is treated as an integer, you can explicitly cast it to the int8 data type using the int8() function. This converts the number to an 8-bit signed integer.

Note: The isinteger() function in MATLAB is used to check if a variable is of the integer data type, not if a number is an integer.

Method 2: By Comparing with the floor() Function

Another approach is to compare the number with its floor value using the floor() function. The floor function rounds a value towards negative infinity and returns the largest integer that is smaller than or equal to the input. If the starting value matches the floor value, the number is an integer.

number =3.5;
isInteger = (number == floor(number));
 
% Display the result
disp(isIneger);

 

The floor function rounds down the number to the nearest integer. If the original number and the rounded-down number are the same, it means that the number is already an integer. In this case, since 3.5 is not equal to 3 (its floor value), the expression (number == floor(number)) will evaluate as false. Thus, isInteger() will be assigned the value 0, which represents false.

Method 3: By Checking the Remainder

You can also check if the remainder of dividing the number by 1 is zero and if the number is an integer if the remainder is zero:

number = 7;
isInteger = (rem(number, 1) == 0); % Returns true
 
% Display the result
disp(isInteger);

 

The rem function calculates the remainder when dividing the number by 1. If the remainder is 0, it indicates that the number is divisible by 1 and therefore an integer. The expression (rem(number, 1) == 0) will evaluate to true in this case. Thus, isInteger() will be assigned the value 1, which represents true.

Method 4: Using the mod() Function:

The remainder of the division operation is calculated using the mod function:

number = 2.25;
isInteger = (mod(number, 1) == 0); % Returns false
 
% Display the result
disp(isInteger);

 

In the code, the mod() function is used to calculate the remainder when the number is divided by 1. If the remainder is equal to 0, it means that the number is divisible by 1 and hence an integer. The expression (mod(number, 1) == 0) returns a logical value of false if the number is not an integer and true if it is. For the number 2.25, the remainder when divided by 1 is not 0, so the expression (mod(number, 1) == 0) evaluates to false, indicating that the number is not an integer.

Conclusion

Checking if a number is an integer in MATLAB is essential for various applications. By using the isinteger() function, comparing it with the floor value, checking the remainder, or using the mod”\ () function, you can easily determine if a number is an integer.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.