MATLAB: Index in position 1 is invalid. Array indices must be positive integers or logical values.

matrix array

kernel = [0 1 0; 1 0 1; 0 1 0]
A = [10 2 10; 10 5 3; 4 2 5]
m = 3;
n = 3;
for i=1:m
for j=1:n
sum = (kernel(m-i,n-j) * A(i-1,j-1))
end
end
Hi everybody,
I got this error ;
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in file (line 22)
t = A(x1,x2);
Why I got this error?

Best Answer

When i=m in your loop you try to access kernel(m-m,n-j) which is kernel(0,m-j), and 0 is an invalid index. Similar problems with all of the other indexing on this line. It is easy to find values of the for-loop indices where each index in your computation is invalid.