Index in position 2 exceeds array bounds. Index must not exceed 1. - correctly indexing a changing element in a matrix

1 次查看(过去 30 天)
So I'm trying to write a for loop that indexes a specific element of a matrix, but the element being indexed changes for each loop. Here's what I have
for tt=19:-1:1
for j=1:15
for k=1:15
for i=info
if M(j,k)>0
p(j,k)=0.9;
elseif M(j,k)<0
p(j,k)=0.1;
elseif M(j,k)==0
p(j,k)=0.5;
end
Fd(i,j,k)= b*(...
m1*(...
p(y(j),z1(k))*(n1*Fd(xp(:,i),zp(j),y(k),tt+1) + (1-n1)*Fd(xpp(:,i),zpp(j),y(k),tt+1))+...
y and z1 are vectors that correctly call upon the element I want when using it in the matrix e.g.
>> M(y(1),z1(2))
ans =
2
I have the vectors written so that they don't go beyond the matrix boundary. z1 = z+1 and the matrix boundary is 15, but I've capped the vector so that z1(15) just returns 15.
I think the problem is coming from my use of p trying to call the element. Any insight as to why it's not working?
  1 个评论
Cris LaPierre
Cris LaPierre 2024-4-18
The error is easy enough to explain, but without a working example, it's hard to say what you should change.
Somewhere your column index exceeds the number of columns of the variable, which the error says is 1. Maybe you are working with a column vector instead of a row vector?
a=(1:5)';
% works
a(3,1)
ans = 3
% your error
a(1,3)
Index in position 2 exceeds array bounds. Index must not exceed 1.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by