"Undefined function or variable 'y'"
2 ビュー (過去 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 件のコメント
採用された回答
Bob Thompson
2018 年 10 月 8 日
編集済み: Bob Thompson
2018 年 10 月 8 日
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 件のコメント
その他の回答 (1 件)
amin ya
2018 年 10 月 8 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!