MATLAB Answers

"Undefined function or variable 'y'"

조회 수: 11(최근 30일)
Here is my code.
x = 1:100 (Creating a vector from 1-100)
if rem(x,2) == 0 (looking for even numbers)
y = x
end
y
Why is it telling me that y is undefined. It's essentially skipping over my if statement

  댓글 수: 0

로그인 to comment.

채택된 답변

Bob Nbob
Bob Nbob 8 Oct 2018
편집: Bob Nbob 8 Oct 2018
It's occurring because for your first number, x = 1, the if statement returns false, and so no value for y is defined, but then it's trying to display a value of y that doesn't exist. I would suggest setting y = 0 initially.

  댓글 수: 0

로그인 to comment.

추가 답변(1개)

amin ya
amin ya 8 Oct 2018
If your condition doesn't hold true., it will skip your if, and it will give an error when you recall y.
You can get the output of y by not puting ";" in front of the y=x. You can also use "else"
if rem(x,2) == 0 (looking for even numbers)
y = x
else
y='not defined'
end

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

제품


릴리스

R2018a

Translated by