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?

3 件のコメント

Jaimin Motavar
Jaimin Motavar 2019 年 7 月 5 日
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;
luis fonseca
luis fonseca 2020 年 10 月 9 日
This is the easiert way:
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')
else
disp('N is even')
end
Steven Lord
Steven Lord 2020 年 10 月 9 日
So Inf is even?
>> s = (-1)^Inf
s =
1
How about NaN?
>> s = (-1)^NaN
s =
NaN
Does this hold for a complex number as well?
>> N = 3+4i;
>> s = (-1)^N; % Not equal to -1

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2012 年 10 月 23 日
編集済み: MathWorks Support Team 2018 年 11 月 9 日

3 投票

See mod and rem

1 件のコメント

Dillen.A
Dillen.A 2020 年 2 月 5 日
編集済み: Dillen.A 2020 年 2 月 5 日
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.

サインインしてコメントする。

その他の回答 (8 件)

Jan
Jan 2012 年 10 月 23 日

2 投票

Care for exceptions:
NaN, Inf, 1e54, int8(-128)
There are some FEX submission for this task, e.g. FEX: parity checker.
luis fonseca
luis fonseca 2020 年 10 月 9 日

2 投票

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')
else
disp('N is even')
end

2 件のコメント

madhan ravi
madhan ravi 2020 年 10 月 9 日
what happens when N is an array?
Howard Lam
Howard Lam 2021 年 10 月 15 日
use .^

サインインしてコメントする。

Matt J
Matt J 2012 年 10 月 23 日

1 投票

if bitget(A,1) %odd
else %even
end

2 件のコメント

Matt J
Matt J 2012 年 10 月 23 日
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 2018 年 10 月 10 日
編集済み: Josh Meyer 2018 年 10 月 10 日
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.

サインインしてコメントする。

Ibn e Adam
Ibn e Adam 2020 年 2 月 18 日

0 投票

% 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 件のコメント

Matt J
Matt J 2020 年 2 月 18 日
編集済み: Matt J 2020 年 2 月 18 日
This is already covered by previous answers...
Achala Bohra
Achala Bohra 2020 年 2 月 24 日
why are even and odd (the values of output) not in inverted commas
Walter Roberson
Walter Roberson 2020 年 2 月 26 日
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 2020 年 2 月 26 日
not in inverted commas
I guess inverted commas = single quotes

サインインしてコメントする。

Anmol  singh
Anmol singh 2020 年 4 月 10 日
編集済み: Anmol singh 2020 年 4 月 10 日

0 投票

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 2020 年 4 月 10 日
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

サインインしてコメントする。

Matt J
Matt J 2020 年 10 月 9 日
編集済み: Matt J 2020 年 10 月 9 日

0 投票

One more way to test even-ness in a scalar, s,
isEven=false;
try, validateattributes(s,"numeric","even"); isEven=true; end,
Howard Lam
Howard Lam 2021 年 10 月 15 日
編集済み: Howard Lam 2021 年 10 月 27 日

0 投票

testN = 10000000;
testvar = round(rand(testN,1)*testN);
tic
output1=rem(testvar,2);
toc
tic
output2=floor(testvar/2) ~= testvar/2;
toc
tic
output3=bitget(testvar,1)==1;
toc
tic
output4 = (-1).^testvar == -1;
toc
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.
EDIT: Tested on AMD Ryzen 5800H 2018b. Updated answer for the case when your variables are already integers so that you do not have to cast first.
testN = 10000000;
testvar = round(rand(testN,1)*testN);
tic
output1=floor(testvar/2) ~= testvar/2;
toc
testvar = uint32(testvar);
tic
output2=rem(testvar,2)==1;
toc
tic
output3=bitget(testvar,1)==1;
toc
tic
output4 = (-1).^testvar == -1;
toc
results in
>> testisoddspeed
Elapsed time is 0.014634 seconds.
Elapsed time is 0.123930 seconds.
Elapsed time is 0.013089 seconds.
Elapsed time is 0.032953 seconds.
Bitget can be slightly faster.

11 件のコメント

Matt J
Matt J 2021 年 10 月 16 日
編集済み: Matt J 2021 年 10 月 16 日
Not sure how cross-platform consistent the comparison will be,
testN = 10000000;
testvar = round(rand(testN,1)*testN);
tic
output1=mod(testvar,2);
toc
Elapsed time is 1.002295 seconds.
tic
output2=floor(testvar/2) ~= testvar/2;
toc
Elapsed time is 0.082257 seconds.
tic
output3=bitget(uint32(testvar),1);
toc
Elapsed time is 0.037725 seconds.
tic
output4 = (-1).^testvar == -1;
toc
Elapsed time is 0.195984 seconds.
Walter Roberson
Walter Roberson 2021 年 10 月 17 日
編集済み: Walter Roberson 2021 年 10 月 17 日
MATLAB Answers figures.
"Warning up" seems to have made a statistical difference for the first time, but not really for the others.
runtest();
Elapsed time is 0.718661 seconds. Elapsed time is 0.075098 seconds. Elapsed time is 0.028231 seconds. Elapsed time is 0.143846 seconds.
runtest();
Elapsed time is 0.626687 seconds. Elapsed time is 0.073036 seconds. Elapsed time is 0.025461 seconds. Elapsed time is 0.139366 seconds.
runtest();
Elapsed time is 0.627738 seconds. Elapsed time is 0.073196 seconds. Elapsed time is 0.025515 seconds. Elapsed time is 0.139189 seconds.
function runtest();
testN = 10000000;
testvar = round(rand(testN,1)*testN);
tic
output1=mod(testvar,2);
toc
tic
output2=floor(testvar/2) ~= testvar/2;
toc
tic
output3=bitget(uint32(testvar),1);
toc
tic
output4 = (-1).^testvar == -1;
toc
end
Walter Roberson
Walter Roberson 2021 年 10 月 17 日
編集済み: Walter Roberson 2021 年 10 月 17 日
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.)
Howard Lam
Howard Lam 2021 年 10 月 18 日
編集済み: Howard Lam 2021 年 10 月 18 日
May I assume the older iMac has an intel processor? I ran on Ryzen 5800H on Matlab 2018b. I have also just tested it on 6700 on 2021b on my college machine. It gives respectively
Elapsed time is 0.291904 seconds.
Elapsed time is 0.058646 seconds.
Elapsed time is 0.311071 seconds.
Elapsed time is 0.176075 seconds.
I actually posted a loop version before the edit, and for the loop version, bitget was the slowest one on 5800H, not mod. All tested on Windows.
Matt J
Matt J 2021 年 10 月 18 日
編集済み: Matt J 2021 年 10 月 18 日
@Howard Lam Make sure the bitget test includes the conversion to uint32.
output3=bitget(uint32(testvar),1)
Walter Roberson
Walter Roberson 2021 年 10 月 18 日
Correct, my older iMac has 3.5 GHz Quad-Core Intel Core i7
My newer iMac has i9 but I haven't really gotten around to installing it yet.
Howard Lam
Howard Lam 2021 年 10 月 27 日
@Matt J I have updated the answer for the case when the variables are integers.
Matt J
Matt J 2021 年 10 月 27 日
@Howard Lam Even when the variables do need to be cast to uint32, you should be finding that bitget is faster.
Walter Roberson
Walter Roberson 2021 年 10 月 27 日
I just tried again on my main machine. I got a fair bit of variation in times, but in this series of tests, the third test (bitget) was consistently the fastest... unlike last time I tested.
Elapsed time is 0.234827 seconds.
Elapsed time is 0.017931 seconds.
Elapsed time is 0.013411 seconds.
Elapsed time is 0.051715 seconds.
Howard Lam
Howard Lam 2021 年 10 月 27 日
@Matt J Untrue in my tests. if I include casting as part of profiling, it takes significantly longer.
@Walter Roberson Did you use the new code that has casting outside of tic toc blocks?
Walter Roberson
Walter Roberson 2021 年 10 月 28 日
This is the code I used:
fprintf('1\n');
runtest();
fprintf('2\n');
runtest();
fprintf('3\n');
runtest();
function runtest();
testN = 10000000;
testvar = round(rand(testN,1)*testN);
tic
output1=mod(testvar,2);
toc
tic
output2=floor(testvar/2) ~= testvar/2;
toc
tic
output3=bitget(uint32(testvar),1);
toc
tic
output4 = (-1).^testvar == -1;
toc
end

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWrite Unit Tests についてさらに検索

質問済み:

2012 年 10 月 23 日

コメント済み:

2021 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by