How to run the following codes.
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Matt Gaidica
on 24 Jan 2019
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!