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
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!