"Index in position 2 exceeds array bounds. Index must not exceed 401" Error. Can someone explain to me why here an error appears?
조회 수: 1 (최근 30일)
이전 댓글 표시
x_exact = randn(size(A,1),L+1); u_exact = randn(size(B,2),L+1);
for k = 1:L
x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);
y_exact(:,k) = C*x_exact(:,k);
end
댓글 수: 2
Dyuman Joshi
2023년 4월 26일
In which line does the error occur?
What are the sizes of variables x_exact, u_exact, A, B and C and what is the value of L?
답변 (2개)
albara
2023년 4월 26일
The error "Index in position 2 exceeds array bounds. Index must not exceed 401" occurs because the size of the x_exact array is not sufficient to store all the values being assigned to it in the loop. Specifically, the loop runs L times, with k taking values from 1 to L. In the second line of the loop, the code tries to assign values to x_exact(:,k+1), which means that on the last iteration of the loop, k takes the value L and x_exact(:,L+1) is accessed. However, x_exact has only L+1 columns, so the index L+1 exceeds the array bounds and causes an error.
To fix this error, you need to increase the size of the x_exact array to have at least L+1 columns. One way to do this is to initialize x_exact with zeros instead of randn and specify the size to be size(A,1),L+1 before the loop:
scss
x_exact = zeros(size(A,1),L+1);
u_exact = randn(size(B,2),L+1);
for k = 1:L
x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);
y_exact(:,k) = C*x_exact(:,k);
end
This will ensure that x_exact has the correct size to store all the values being assigned to it in the loop, and the error should be resolved.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
댓글 수: 2
albara
2023년 4월 26일
You're welcome!
If you initialize x_exact with randn and the same dimensions as mentioned, the code will run without errors as long as L is not too large, because the size of x_exact will be sufficient to store all the values being assigned to it in the loop. However, the initial values in x_exact will be random and may not satisfy any initial conditions or constraints that the system may have. Therefore, it is often better to initialize x_exact with a known or desired initial state, such as all zeros or some other value that is appropriate for the specific system being simulated.
In this case, initializing x_exact with zeros is a good choice because it ensures that the initial state is known and satisfies any initial conditions or constraints. Additionally, it has the same size as u_exact and makes it easier to combine x_exact and u_exact into a single matrix for later analysis, if desired.
VBBV
2023년 4월 26일
편집: VBBV
2023년 4월 26일
I guess, L may be a vector or array which keeps changing in your program. Try the below
x_exact = randn(size(A,1),length(L)+1);
u_exact = randn(size(B,2),length(L)+1);
%
for k = 1:length(L)
%-->>
x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);
y_exact(:,k) = C*x_exact(:,k);
end
댓글 수: 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!