Index exceeds matrix dimensions.

Daily 10*10 grid precipitation data of 6 days (10*10*6) includes NaN, zeros and negative values. We are trying to replace the NaN values with surrounding cell values.The below script is giving two errors and could not fix it, ("Subscript indices must either be real positive integers or logical") ("Index exceeds matrix dimensions") And the script is as
for i = 2:10;
for j = 2:10;
for k = 2:6;
if isnan(tstsample(i,j,k))
tmp = tstsample(i-1:i+1,j-1:j+1,k);
tstsample(i,j,k) = nanmean(tmp);
end
end
end
end
Need help to solve these issues. I found few materials but not exact one https://ch.mathworks.com/matlabcentral/answers/120517-index-exceeds-matrix-dimensions-error
Thank you in advance

 Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Mayo de 2018

0 votos

Suppose it finds a nan when i=10. Then you would try to access from i-1:i+1 = 9:11. But the maximum for your first dimension is 10.
You could run the code for i=2:9 but you have the general problem that you cannot fix nan that are on the boundary.

6 comentarios

Shakir Hussain
Shakir Hussain el 1 de Mayo de 2018
Intended to replace the NaN with mean of surrounding values according to this picture
Ameer Hamza
Ameer Hamza el 1 de Mayo de 2018
But what if Nan is on the boundary (e.g. leftmost column)? How will you find the average of neighbor, since there is no neighbor to the left?
Shakir Hussain
Shakir Hussain el 3 de Mayo de 2018
Editada: Shakir Hussain el 3 de Mayo de 2018
The left and right corner are covering by this code
for i = 2:11;
for j = 2:11;
for k = 1:9;
if isnan(tstsample(i,j,k));
tstsample(i,j,k) =nanmean(nanmean(tstsample(i-1:i+1:end,j-1:j+1:end,k)));
end
end
end
end
Only the first row is not capturing.
Walter Roberson
Walter Roberson el 3 de Mayo de 2018
Do not use the code you posted. Use one of Jan's two solutions that I linked to: Jan already took into account how to handle edges and corners.
Shakir Hussain
Shakir Hussain el 3 de Mayo de 2018
Editada: Shakir Hussain el 3 de Mayo de 2018
Codes of Jan`s is dealing well the right and left (columns) corner but did not cover the up and down edge (first and last row), I tired all of his codes.
You are mistaken.
In the below, I used Jan's conv2 version, but put on some data creation and some display code to emphasize nans. If you run this code you will see nans on the left highlighted in red. You will not see any nans on the right, but if you look at the code you can see that the display logic is the same so there would be red if the nans existed.
If you examine the values such as comparing x(end-1:end,end-1:end) to X(end-1:end,end-1:end) then you will see that X (the version without nans) has properly calculated X(end,end) based upon the average of the surrounding areas.
if ~exist('x', 'var')
x = randi([0 255], [64 40]);
x(randperm(numel(x),15)) = nan;
x(1,5) = nan; x(end,end) = nan;
end
subplot(1,2,1);
image(uint8(x), 'AlphaData', 0+~isnan(x));
colormap(gray(256));
set(gca,'color', 'r');
[ny, nx] = find(isnan(x));
hold on
%scatter(nx, ny, 'go')
hold off
X = x;
nanX = isnan(X);
X(nanX) = 0;
mask = [1 1 1; 1 0 1; 1 1 1];
means = conv2(X, mask, 'same') ./ ...
conv2(~nanX, mask, 'same');
X(nanX) = means(nanX);
subplot(1,2,2);
image(uint8(X), 'AlphaData', 0+~isnan(X));
colormap(gray(256));
set(gca,'color', 'r');
[nY, nX] = find(isnan(X));
hold on
%scatter(nX, nY, 'go')
hold off

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 1 de Mayo de 2018
Editada: Walter Roberson el 2 de Mayo de 2018

0 votos

3 comentarios

Shakir Hussain
Shakir Hussain el 2 de Mayo de 2018
Editada: Walter Roberson el 2 de Mayo de 2018
<<
The first and last row did not access by this script, The last row can access by this way
if isnan(tstsample(i,j,k))==1;
tstsample(i,j,k) =nanmean(nanmean(tstsample(i-1:i+1:end,j-1:j+1:end,k)));
end
But do not know , how to include first row in this calculation.
Walter Roberson
Walter Roberson el 2 de Mayo de 2018
The point was to use one of Jan's solutions posted there.
This has no connection to the current Question, and should be posted as its own Question.
a = rgb2gray(a);
You convert rgb image, a to gray, and you replace a with that gray image.
a_g = a( :, :, 2 );
Now you want to access the green plane of the gray image you just created.
Perhaps you should extract the green before converting to gray. Or perhaps you should use a different variable name for the gray image so that you still have the rgb available if you need it.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Preprocessing en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 1 de Mayo de 2018

Comentada:

el 11 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by