Error : Index in position 1 is invalid

3 views (last 30 days)
WARRIOR24
WARRIOR24 on 5 Apr 2021
Edited: DGM on 5 Apr 2021
I am trying to keep my index(s) row and/or col positive so I added some if statments to reinitiat row - 1 and col - 1 to equatl 1. Also vise versa for row+1 = 10 and col+1=10 to make then just equal Nx/Nx. I thought I coded it correctly but I keep getting an error and cannot think of anything else.
Nx = 9;
Ny = 9;
domain=zeros(Nx,Ny);
cons1=epsilon_0/(epsilon_0+epsilon_r);
cons2=epsilon_r/(epsilon_0+epsilon_r);
for row = 1:Ny
for col = 1:Nx
if row > Ny
row = row - 1;
end
if row < 1
row = row+1;
end
if col > Nx
col = col-1;
end
if col < 1
col = col-1;
end
v1 = domain(row, col+1);
v2 = domain(row-1, col);
v3 = domain(row, col-1);
v4 = domain(row+1, col);
if row == (1/3)*(Ny+1) || row ==(2\3)*(Ny)
domain(row,col) = cons1*v1+cons2*v3+(1/4)*(v2+v4);
elseif row == 1
domain(0, col)=(1/4)*(v1+v3+2*v4);
elseif col == 1
domain(0, col)=(1/4)*(2*v1+v2+v4);
else
domain(row,col) = (1/4)*(v1+v2+v3+v4)
end
end
end
I keep getting this error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in HW6dif (line 64)
v2 = domain(row-1, col);

Accepted Answer

DGM
DGM on 5 Apr 2021
Edited: DGM on 5 Apr 2021
Don't go changing loop iterators inside the loop unless you want to cause problems. It's entirely unnecessary here anyway. If you state that:
for col = 1:Nx
then col will never be less than 1 or greater than Nx. You don't need to clamp them. So we can get rid of that part.
Nx = 9;
Ny = 9;
domain=zeros(Nx,Ny);
cons1=epsilon_0/(epsilon_0+epsilon_r);
cons2=epsilon_r/(epsilon_0+epsilon_r);
for row = 1:Ny
for col = 1:Nx
v1 = domain(row, col+1);
v2 = domain(row-1, col);
v3 = domain(row, col-1);
v4 = domain(row+1, col);
if row == (1/3)*(Ny+1) || row ==(2\3)*(Ny)
domain(row,col) = cons1*v1+cons2*v3+(1/4)*(v2+v4);
elseif row == 1
domain(0, col)=(1/4)*(v1+v3+2*v4);
elseif col == 1
domain(0, col)=(1/4)*(2*v1+v2+v4);
else
domain(row,col) = (1/4)*(v1+v2+v3+v4)
end
end
end
Of course, it still won't run, because that wasn't really the problem. The line you mentioned is the problem.
v2 = domain(row-1, col);
On the first pass, row and col are 1, so it's looking for domain(0,1), which isn't a valid location in an array. Similarly, there are lines like these that use zero indices:
domain(0, col)=(1/4)*(v1+v3+2*v4);
...
domain(0, col)=(1/4)*(2*v1+v2+v4);
Since I don't really know what this code is supposed to be doing, I can't say how to fix it. You might be able to offset the starting index in the loop definition. You might be able to pad the array. You might be able to limit the indexes when using them:
v2 = domain(max(row-1,1), col);
The propriety of any of these options depends on what is actually supposed to be done.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!