Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comment
Cris LaPierre
Cris LaPierre on 18 Apr 2024
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.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!