MATLAB: “Undefined function or variable ‘y'”

conditional statementsif statementMATLABundefined

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

Best Answer

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.