MATLAB Answers

Index exceeds matrix dimensions.

4 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 1 May 2018
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 Comments

Show 3 older comments
Walter Roberson
Walter Roberson on 3 May 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 on 3 May 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.
Walter Roberson
Walter Roberson on 3 May 2018
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

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 1 May 2018
Edited: Walter Roberson on 2 May 2018

  3 Comments

Shakir Hussain
Shakir Hussain on 2 May 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 on 2 May 2018
The point was to use one of Jan's solutions posted there.
Walter Roberson
Walter Roberson on 11 May 2020
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.

Sign in to comment.