MATLAB Answers

0

Index exceeds matrix dimensions.

Asked by Shakir Hussain on 1 May 2018
Latest activity Commented on by Walter Roberson
on 3 May 2018
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.

2 Answers

Answer by Walter Roberson
on 1 May 2018
 Accepted Answer

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

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.
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

Sign in to comment.


Answer by Walter Roberson
on 1 May 2018
Edited by Walter Roberson
on 2 May 2018

  2 Comments

<<
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.
The point was to use one of Jan's solutions posted there.

Sign in to comment.