How to make a variable an even number?

69 ビュー (過去 30 日間)
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 11 日
コメント済み: madhan ravi 2019 年 9 月 12 日
if i have variable an even number is there any direct command can be used please mention with an example

採用された回答

madhan ravi
madhan ravi 2019 年 9 月 11 日
編集済み: madhan ravi 2019 年 9 月 11 日
Is your question "How to check if a variable consists of even numbers?"
Google "even number mod matlab"
  7 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 12 日
I don't have enough experience with Simulink.
Ali Mukhtar
Ali Mukhtar 2019 年 9 月 12 日
ok np

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2019 年 9 月 12 日
編集済み: John D'Errico 2019 年 9 月 12 日
If you just want to check if a variable X is an even number, then just check the value of mod(x,2). If the result is zero, then X was even.
X = randi(10,1,5)
X =
4 10 8 9 7
mod(X,2)
ans =
0 0 0 1 1
So the first three elements of X were even numbers, because the modulus base 2 were zero for those elements. Here, zero tell you even. 1 tells you odd. If you want a result that is true for even numbers, then do something like this:
mod(X,2) == 0
ans =
1×5 logical array
1 1 1 0 0
As you can see, the result is now true for the even numbers.
If you want to see if all, or any of them were even, then do one of these as appropriate:
all(mod(X,2) == 0)
ans =
logical
0
any(mod(X,2) == 0)
ans =
logical
1
So they were not all even, but SOME of them were even.
  1 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 12 日
+1 , smooth and informative explanation as always :)

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by