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일

0 개 추천

Is your question "How to check if a variable consists of even numbers?"
Google "even number mod matlab"

댓글 수: 7

Ali Mukhtar
Ali Mukhtar 2019년 9월 11일
it only tells how we can check i want to make it even value i have already seen it
madhan ravi
madhan ravi 2019년 9월 11일
편집: madhan ravi 2019년 9월 11일
So what is the question if you want to make a variable consisting of even numbers then:
x = 1:3 ; % an example
X = [x,zeros(mod(numel(x),2))] % if the variable is odd then the last element is appended as zero, is this what you want?
Ali Mukhtar
Ali Mukhtar 2019년 9월 12일
No i want to just check two variables S and P
If S< P then make P an even number
madhan ravi
madhan ravi 2019년 9월 12일
편집: madhan ravi 2019년 9월 12일
Are you inducing me to write a full code for your homework?, well then I am sorry. I have suggested a solution to make a variable containing even number of elements. Your professor/teacher can easily find this forum as you did, so finally I'm not the one who's going to into trouble :/
madhan ravi
madhan ravi 2019년 9월 12일
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?
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일

1 개 추천

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 :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Time Series Objects에 대해 자세히 알아보기

질문:

2019년 9월 11일

댓글:

2019년 9월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by