Index exceeds matrix dimensions.
조회 수: 1 (최근 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!