MATLAB Answers

how to find out if a number is even or not

조회 수: 1,997(최근 30일)
divya r
divya r 23 Oct 2012
댓글: Walter Roberson 10 Apr 2020
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?

  댓글 수: 1

Jaimin Motavar
Jaimin Motavar 5 Jul 2019
you can do it this in matlab by the very simple way by using implicit function rem(a,b) , where a is devided by b.
for example.
r1=rem(4,2)
r=0;
r2=rem(9,2)
r2=1;

Sign in to comment.

채택된 답변

Walter Roberson
Walter Roberson 23 Oct 2012
편집: MathWorks Support Team 9 Nov 2018
See mod and rem

  댓글 수: 1

Dillen.A
Dillen.A 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.

추가 답변(5개)

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

  댓글 수: 0

Sign in to comment.


Matt J
Matt J 23 Oct 2012
if bitget(A,1) %odd
else %even
end

  댓글 수: 2

Matt J
Matt J 23 Oct 2012
Note that solutions based on REM and MOD have certain non-robustness to large numbers, though I never quite understood why:
>> mod(bitmax+[1:8],2) %all are even
ans =
0 0 0 0 0 0 0 0
Josh Meyer
Josh Meyer 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.


Ibn e Adam
Ibn e Adam 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
output=even;
else
output=odd;
end
end

  댓글 수: 4

표시 이전 댓글 수: 1
Achala Bohra
Achala Bohra 24 Feb 2020
why are even and odd (the values of output) not in inverted commas
Walter Roberson
Walter Roberson 26 Feb 2020
Ibn e Adam did not define the variables "even" or "odd" so we do not know what datatypes would be returned.
Matt J
Matt J 26 Feb 2020
not in inverted commas
I guess inverted commas = single quotes

Sign in to comment.


Anmol  singh
Anmol singh 10 Apr 2020
편집: Anmol singh 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.
example:
int i=5;
binary form i= 0101
now use & operator
int j=i&1;[0101&1]//
here j have 0001;

  댓글 수: 1

Walter Roberson
Walter Roberson 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;
else
c = false;
end
elseif B ~= 0
c = false;
else
c = false;
end
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
bitand(5,1)
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.

태그


Translated by