필터 지우기
필터 지우기

"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
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?
Edfred
Edfred 2023년 4월 26일
In the line: x_exact(:,k+1) = A*x_exact(:,k) + B*u_exact(:,k);

댓글을 달려면 로그인하십시오.

답변 (2개)

albara
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
Edfred
Edfred 2023년 4월 26일
Thank you very much for your detailed answer :)
Does that mean that if I intialize x_exact with randn and the same dimensions as mentioned it does not work and that I can run it just with zeros as initialization?
albara
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
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by