"Undefined function or variable 'y'"
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Daniel Schilling
il 8 Ott 2018
Modificato: Bob Thompson
il 8 Ott 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
0 Commenti
Risposta accettata
Bob Thompson
il 8 Ott 2018
Modificato: Bob Thompson
il 8 Ott 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 Commenti
Più risposte (1)
amin ya
il 8 Ott 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 Commenti
Vedere anche
Categorie
Scopri di più su Get Started with MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!