Index exceed matrix dimensions.

1 次查看(过去 30 天)
SUDIPTA GHOSH
SUDIPTA GHOSH 2014-3-10
编辑: Patrik Ek 2014-3-13
I wrote the following codes, but after commanding it for a run, it is showing that index exceeds matrix dimension and error in testmisr(line 11) if od(a,b,c,d,e)==-9.999. the codes:
for i = 1:30;
filename_apr = ['/Users/Sudipta/Documents/misr daily data/mar00-feb01/061703666281887/MISR_AMI_CGAS_APR_',num2str(i),'_2000_F15_0031.hdf'];
od(i,:,:,:,:,:) = hdfread('F:\Modis_TERRA_daily\MISR_AM1_CGAS_APR_01_2000_F15_0031.hdf', '/AerosolParameterAverage/Data Fields/Optical depth average', 'Index', {[122 538 1 2 1],[1 1 1 1 1],[12 18 1 1 6]});
for a = 1:12;
for f = 1:31
for b = 1:18;
for c = 1:1;
for d = 1:1;
for e = 1:6;
if od(f,a,b,c,d,e)== -9999;
od(f,a,b,c,d,e) = NaN;
else if od(f,a,b,c,d,e)==-9.999;
od(f,a,b,c,d,e) = NaN;
else
od(f,a,b,c,d,e)=od(f,a,b,c,d,e);
end
end
end
end
end
end
end
  1 个评论
Chandrasekhar
Chandrasekhar 2014-3-10
index is trying to access the matrix element which doesnt exist. index > length(array/matrix)

请先登录,再进行评论。

回答(1 个)

Patrik Ek
Patrik Ek 2014-3-10
编辑:Patrik Ek 2014-3-10
You do not need make all these for loops for this. Instead of
for a = 1:12;
for f = 1:31
for b = 1:18;
for c = 1:1;
for d = 1:1;
for e = 1:6;
if od(f,a,b,c,d,e)== -9999;
od(f,a,b,c,d,e) = NaN;
else if od(f,a,b,c,d,e)==-9.999;
od(f,a,b,c,d,e) = NaN;
else
od(f,a,b,c,d,e)=od(f,a,b,c,d,e);
end
end
end
end
end
end
do something like,
od(od == -9999 | od==-9.999) = nan;
This will not only solve the index out of bounds problem, but speed up your code quite a lot as an extra bonus.
BR/Patrik
  1 个评论
Patrik Ek
Patrik Ek 2014-3-13
编辑:Patrik Ek 2014-3-13
If you is still in to doing loops I would then suggest
od = od(:);
% do operations
od = reshape(od,len_a,len_b,...)
Since matlab works absolutely best for columns.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by