Index in position 1 is invalid. Array indices must be positive integers or logical values.
Ältere Kommentare anzeigen
% 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?
10 Kommentare
Torsten
am 20 Nov. 2022
If I should make a guess: dy*i/dy1 and (dx*j)/dx1 are not integer values as required to index into a matrix.
Kushagra Saurabh
am 20 Nov. 2022
Torsten
am 20 Nov. 2022
What are the numerical values of dy/dy1 and dx/dx1 ?
Kushagra Saurabh
am 21 Nov. 2022
Torsten
am 21 Nov. 2022
First condense A to a 100x100 matrix (e.g. by taking the mean of subsequent 10x10 matrices). Then apply your loop.
Kushagra Saurabh
am 21 Nov. 2022
A = rand(1000);
B = zeros(100);
for i = 1:10
for j = 1:10
B(i,j) = mean(mean(A((i-1)*10+1:i*10,(j-1)*10+1:j*10)));
end
end
B
Kushagra Saurabh
am 21 Nov. 2022
Torsten
am 21 Nov. 2022
What is the purpose of your code ? Do you solve Laplace's equation on two different meshes ?
Kushagra Saurabh
am 21 Nov. 2022
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 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
3 Kommentare
Kushagra Saurabh
am 21 Nov. 2022
Walter Roberson
am 21 Nov. 2022
imresize() or resample()
Walter Roberson
am 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.
Kategorien
Mehr zu Polygons finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!