"Undefined function or variable 'y'"

2 Ansichten (letzte 30 Tage)
Daniel Schilling
Daniel Schilling am 8 Okt. 2018
Bearbeitet: Bob Thompson am 8 Okt. 2018
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

Akzeptierte Antwort

Bob Thompson
Bob Thompson am 8 Okt. 2018
Bearbeitet: Bob Thompson am 8 Okt. 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.

Weitere Antworten (1)

amin ya
amin ya am 8 Okt. 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

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by