How can I fix this?
1 次查看(过去 30 天)
显示 更早的评论
clear all; close all; clc; format compact
x1 = 2
sqrt = sqrt(x1)
x2 = sqrt^2
y1 = 3
st = sqrt(y1)
y2 = st1^2
Index exceeds the number of array elements. Index must not exceed 1.
Error in Debugging_7_Eduardo_Gallegos (line 6)
st = sqrt(y1)
I'm not sure what to do to fix this error
0 个评论
采纳的回答
dpb
2022-10-25
编辑:dpb
2022-10-25
Don't alias the builtin sqrt function by assign a value to it and thereby turning the reference to sqrt into a variable.
sqrt = sqrt(x1)
just wiped out the function of the same name by hiding it behind the variable of the same name.
x1 = 2;
sqr_x = sqrt(x1);
x2 = sqr_x^2;
...
will save your bacon.
MORAL: Don't EVER use a builtin function name as a variable!!!
NOTA BENE: You'll then need to
clear sqrt
to rid the workspace of the variable before going on...your first line I didn't put into the code will do the deed as well, but is way overkill for almost all uses (excepting if the instructor makes it a requirement for grading purposes).
2 个评论
dpb
2022-10-25
Don't feel bad...everybody does that at some point or another in their learning curve...and sometimes the most obvious is the hardest to see having made a blunder.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!