Index exceeds matrix dimensions.

1 次查看(过去 30 天)
why??
D = zeros(size(M,1),size(M,1),size(M,2));
for k=(size(M,2)-1):-1:1
P_pred = A(:,:,k) * P(:,:,k) * A(:,:,k)' + Q(:,:,k);
D(:,:,k) = P(:,:,k) * A(:,:,k)' / P_pred;
M(:,k) = M(:,k) + D(:,:,k) * (M(:,k+1) - A(:,:,k) * M(:,k));
P(:,:,k) = P(:,:,k) + D(:,:,k) * (P(:,:,k+1) - P_pred) * D(:,:,k)';
end
the error is for line P_pred = A(:,:,k) * P(:,:,k) * A(:,:,k)' + Q(:,:,k);

回答(1 个)

Stephen23
Stephen23 2017-9-17
编辑:Stephen23 2017-9-17
Because you are trying to access some part of an array that does not exist. Just like this:
>> A = rand(4,3,2);
>> A(:,:,99)
??? Index exceeds matrix dimensions.
>>
My example array only has size 2 along the third dimension, so what do you expect to happen if I try to access the 99th position along the third dimension?
If you really want to learn how to write and debug your own code then one very important skill is to start actually looking at your code. You could have very easily figured this error out yourself by looking at each of the variables, and trying each part of that line: break it down into atomic operations, and trying them in the command line. And by reading the error message of course.

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by