7 views (last 30 days)

madhan ravi
on 11 Sep 2019

Edited: madhan ravi
on 11 Sep 2019

Is your question "How to check if a variable consists of even numbers?"

Google "even number mod matlab"

madhan ravi
on 12 Sep 2019

Ali Mukhtar comments:

no sir thats not the case , this my own work not any assignment. and thanks for your help i have solved the problem... can you please tell how can we display simulink fuction values in matlab command window?

John D'Errico
on 12 Sep 2019

Edited: John D'Errico
on 12 Sep 2019

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.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.