Index in position 2 exceeds array bounds. Index must not exceed 1. - correctly indexing a changing element in a matrix
12 ビュー (過去 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
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)
% your error
a(1,3)
回答 (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!