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!