MATLAB Answers

how to find out if a number is even or not

4,000 views (last 30 days)
divya r
divya r on 23 Oct 2012
Edited: Walter Roberson about 15 hours ago
I know in C language, for any number x using x%2 will calculate the remainder when x is divided by 2, which will help decipher whether its even or not.
How can I do this in matlab?
Steven Lord
Steven Lord on 9 Oct 2020
So Inf is even?
>> s = (-1)^Inf
s =
How about NaN?
>> s = (-1)^NaN
s =
Does this hold for a complex number as well?
>> N = 3+4i;
>> s = (-1)^N; % Not equal to -1

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Oct 2012
Edited: MathWorks Support Team on 9 Nov 2018
See mod and rem
  1 Comment
Dillen.A on 5 Feb 2020
A quick example:
A = [-2 -1 0 1 2 3 4 5 6]; % A is your value or matrix
IS_EVEN = ~mod(A,2)
Which is the same as
IS_EVEN = ~bitget(abs(A),1)
And the same as
IS_EVEN = ~rem(A,2)
You can use logical() instead of ~ (isnot) for ODD, should you want booleans. Also bitget() does not work for negative integers, hence abs().
A warning though; ONLY bitget() will throw an error if an element in A is not an integer! the others will output 'odd' for fractions.
Unless you will repeat this many many times, the speed is not relevant. Otherwise, you should vectorize.

Sign in to comment.

More Answers (8)

Jan on 23 Oct 2012
Care for exceptions:
NaN, Inf, 1e54, int8(-128)
There are some FEX submission for this task, e.g. FEX: parity checker.

Matt J
Matt J on 23 Oct 2012
if bitget(A,1) %odd
else %even
Josh Meyer
Josh Meyer on 10 Oct 2018
In more recent versions of MATLAB, bitmax was replaced by flintmax. This is the largest consecutive floating point number. After flintmax, the value of eps is larger than 1 (slowly increasing in powers of 2), so representable numbers larger than flintmax are no longer consecutive.
So, the reason all of those numbers are even is because flintmax is an even number and the spacing between numbers is eps(flintmax) = 2.

Sign in to comment.

luis fonseca
luis fonseca on 9 Oct 2020
This is the easiert way guys, its just math from highschool
N = 1; % number you want to know if even or odd
%% create an expression
s = (-1)^N;
%% if s = -1, the N is odd, else N is even
if s == -1
disp('N is odd')
disp('N is even')

Ibn e Adam
Ibn e Adam on 18 Feb 2020
% function to find even/odd
% n is input number for this function
function output=even_or_odd(n)
if rem(n,2)==0
Matt J
Matt J on 26 Feb 2020
not in inverted commas
I guess inverted commas = single quotes

Sign in to comment.

Anmol  singh
Anmol singh on 10 Apr 2020
Edited: Anmol singh on 10 Apr 2020
A givennumber is even or odd for this we use & operator.
if any number is odd it must have right most bit 1.
int i=5;
binary form i= 0101
now use & operator
int j=i&1;[0101&1]//
here j have 0001;
  1 Comment
Walter Roberson
Walter Roberson on 10 Apr 2020
This does not work in MATLAB. In MATLAB, the operation
c = A & B
is equivalent to
if A ~= 0
if B ~= 0
c = true;
c = false;
elseif B ~= 0
c = false;
c = false;
Yes, this could be made more efficient, but this models the & operator. The more efficient operation is &&
Now notice that this is not a bitwise operation. 5&1 is not binary 0101 & 0001 giving 0001: instead it is (5~=0) and (1 ~= 0)
The MATLAB equivalent to what you are discussing is the bitand() operator
But if you are going to do that, you might as well just ask for the last bit directly:
bitget(5,1) %the 1 is a bit number with LSB being #1

Sign in to comment.

Matt J
Matt J on 9 Oct 2020
Edited: Matt J on 9 Oct 2020
One more way to test even-ness in a scalar, s,
try, validateattributes(s,"numeric","even"); isEven=true; end,

Howard Lam
Howard Lam on 15 Oct 2021 at 6:30
Edited: Howard Lam on 15 Oct 2021 at 6:36
testN = 10000000;
testvar = round(rand(testN,1)*testN);
output2=floor(testvar/2) ~= testvar/2;
output4 = (-1).^testvar == -1;
The above produces the output
>> testisoddspeed
Elapsed time is 0.101100 seconds.
Elapsed time is 0.010721 seconds.
Elapsed time is 0.054311 seconds.
Elapsed time is 0.040362 seconds.
Walter Roberson
Walter Roberson about 2 hours ago
Figures for my (older) iMac, the machine I use most often:
Elapsed time is 0.248780 seconds.
Elapsed time is 0.023129 seconds.
Elapsed time is 0.031683 seconds.
Elapsed time is 0.062816 seconds.
Note that the second item is the fastest, where-as the MATLAB Answers machine, the second is about twice as expensive as the third.
(I ran again to verify the speed was not an accident.)

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!