"Index in position 2 exceeds array bounds. Index must not exceed 401" Error. Can someone explain to me why here an error appears?
2 次查看(过去 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!