How to run the following codes.
1 次查看(过去 30 天)
显示 更早的评论
what is error in following codes
for i=1:12
if i = 12
x1 = 9
end
x1 = [1];
x2 = [3];
x1(i) = x1(i-1) - x2;
x = [x1 x2]
end
When i run it it shows following error please help me to solve error.
Array indices must be positive integers or logical values.
0 个评论
采纳的回答
Matt Gaidica
2019-1-24
MATLAB uses one-based indexing. Therefore, in your code, on the first iteration where i = 1, the line marked is trying to index x1 at the zero position:
for i = 1:12
if i == 12
x1 = 9
end
x1 = [1];
x2 = [3];
x1(i) = x1(i-1) - x2; % <-- error here
x = [x1 x2]
end
There are still issues with your code, but it's hard to know what the fix is without more context. Also, note that i is reserved as an imaginary number in MATLAB, so it's recommended to use ii or another variable as your iterator.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!