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

11 views (last 30 days)
Kushagra Saurabh on 20 Nov 2022
Commented: Kushagra Saurabh on 23 Nov 2022
% calculation of Emax
T_epsilon = zeros(Nx,Ny);
for i =2:Nx-1
for j = 2:Ny-1
T_epsilon(i,j) = T(i,j) - A((dy*i)/dy1,(dx*j)/dx1);
E_max = max(abs(T_epsilon));
disp(E_max);
end
end
I used this code to calculate a difference between two matrices T of order 101-by-101 and A of order 1001-by-1001, for the corresponding value points of T matrices such that the resultant matrix will be of order 101-by-101. But I'm getting an error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
in line:T_epsilon(i,j) = T(i,j) - A((dy*i)/dy1,(dx*j)/dx1);
Where have I gone wrong?
Kushagra Saurabh on 21 Nov 2022
Yes, and I have to find the maximum value of truncation error...

Image Analyst on 20 Nov 2022
Edited: Image Analyst on 20 Nov 2022
You swapped x and y. Remember x is columns which is the second index, not the first index of a matrix. Matrixes are references (row, column) or (y, x), not (x,y) which you are trying to do.
Not sure of your formula so look at it carefully but the code should look something like this:
[Ny, Nx] = size(A); % Nx IS NOT THE FIRST RETURN ARGUMENT!!!
% calculation of Emax
T_epsilon = zeros(Nx,Ny);
for col = 2 : Nx-1
for row = 2 : Ny-1
T_epsilon(row, col) = T(row, col) - WHATEVER....
E_max = max(abs(T_epsilon));
fprintf('E_max = %f\n', E_max);
end
end
Kushagra Saurabh on 23 Nov 2022
Thank you sir

Walter Roberson on 20 Nov 2022
A((dy*i)/dy1,(dx*j)/dx1)
having a division in a calculation of coordinates is very often a problem. You can sometimes get away with it if you are working with pure integers and there is reason why the divisor is known to divide in exactly, such as if the divisor is 1, but if there are any non-integer quantities involved in the calculation then at some point you will encounter problems with round-off error giving you an index that is not exactly an integer.
It looks to me as if you are doing a matrix rescaling operation. If so then typically you would need to calculate each output as a linear combination of adjacent matrix elements. For example if you had coordinates (12.9, 15.4) then you would not round those to (13,15) and would instead calculate A(12,15)*0.1*0.6 + A(13,15)*0.9*0.6 + A(12,16)*0.1*0.4 + A(13,16)*0.9*0.4
Walter Roberson on 21 Nov 2022
A((dy*j)/dy1,(dx*i)/dx1)
That is the wrong way to process arrays; the indices are very likely not going to be integers. You could round() or floor() or ceil() to be sure they are integers, but you have to ask yourself whether that is really what you want to do.
Suppose you had an image that is all black except that every 10'th pixel horizontally and vertically is white, and you are downsizing it 10 to 1. So suppose it just happens that (dy*j)/dy1 and (dx*i)/dx1 are exact integers so that in this hypothetical situation you do not need to worry about rounding coordinates or interpolating between adjacent pixels.
Now in this hypothetical situation, would downsizing by taking every 10th pixel be the right thing to do?
You have two possibilities in the hyptothetical situation: either the integer coordinates you pick out happen to index the white pixels every time, or else the integer coordinates happen to index black pixels every time. The first would result in an all-white output: is an all-white output a fair representation of an image that is 99% black? The second would result in an all-black output: is an all-black output a fair representation of an image that regularly has some white in it?
Now suppose that you have the same input image that is white at locations where the coordinates are multiples of 10, but your downsizing happens to be picking out every 11'th pixel. (11,11) is black, (11,22) is black, (11,33) is black... eventually you get to (11,110) which might potentially be white because the 110 is a multiple of 10, but because the 11 is not a multiple of 10 it is still not white. So the output would be all black except at what was originall (110,110), (110,220), (110,330) and other locations that are multiples of 110 on both axes. Is that a fair representation?
Some methods of downsizing images do not index a single pixel as the source, and instead take the mean2() of a block of input pixels. So in the case of 10:1 reduction, with 99 of the 100 pixels in the 10 x 10 block being black and 1 being white, the output would be (W/100) where W is the intensity associated with a white pixel. If you are using uint8 where white pixels are 255, that would be (255/100) which would round to 3 (out of 255) . Almost black but slightly brighter than pure black. You would get out the average intensity of the block, which in some applications might be appropriate.
Some resizing techniques involve taking a fourier of wavelet representation of the image array and reconstructing at a subset of locations.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by