Index exceeds matrix dimensions.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2018-5-1
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 个评论
Walter Roberson
2018-5-3
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
更多回答(1 个)
Walter Roberson
2018-5-1
编辑:Walter Roberson
2018-5-2
3 个评论
Walter Roberson
2020-5-11
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation of 2-D Selections in 3-D Grids 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!