How can I fix an 'index out of bounds because size' when the array is the right size?
2 views (last 30 days)
Show older comments
When I try to run my program I get the error "Attempted to access x(1,2); index out of bounds because size(x)=[4,1].". I have checked the size of x using size(x), and I get [4,2]. Why would MATLAB be reading the size differently? Is there an error somewhere(or everywhere) in my code?
Here are my m files and the error
4 Comments
Image Analyst
on 7 May 2014
Edited: Image Analyst
on 7 May 2014
What? That's not what I'm talking about. The middle picture of modelfun say that line 20 is "x3=....." while the error message in red at the bottom says that line 20 of modelfun is "x1=.....". So which is it, x1 or x3?
Accepted Answer
Image Analyst
on 7 May 2014
Perhaps you mixed up rows and columns. Put this before you start assigning all the x's
whos x
x
size(x)
whos k
k
More Answers (1)
BMAN
on 13 Dec 2017
when i am trying to run this prgram it is continously showing the error
Attempted to access BLT_elnino(1,21,1); index out of bounds because size(BLT_elnino)=[144,20,22]. how to solve this problem can anyone help me out??
temp=ncread('TEMP_ECMWFORAS41961-2015.nc','THETAO'); sal=ncread('SAL_ECMWFORAS41961-2015.nc','SO'); lat=ncread('SAL_ECMWFORAS41961-2015.nc','LAT96_115'); lon=ncread('SAL_ECMWFORAS41961-2015.nc','LON79_100'); time=ncread('SAL_ECMWFORAS41961-2015.nc','TIME'); lev=ncread('SAL_ECMWFORAS41961-2015.nc','LEV'); temp=permute(temp,[4,3,2,1]); sal=permute(sal,[4,3,2,1]);
num=datenum(1961,1:660,1,0,0,0); % create date vector str=str2num(datestr(num,'yyyy')); % create date string
el=[1965,1966,1972,1973,1982,1983,1987,1988,1991,1992,1997,1998];
la=[1973,1974,1975,1976,1988,1989,1999,2000,2007,2008,2010,2011];
El_Nino=[]; La_Nina=[]; BLT_elnino=NaN(12*12,20,22); BLT_lanina=NaN(12*12,20,22);
for i=1:144 BLT_el_avg(:,:)=nanmean(squeeze(BLT_elnino(i:144,:,:))); BLT_la_avg(:,:)=nanmean(squeeze(BLT_lanina(i:144,:,:)));
end
for i=1:144
for x=1:22
for y=1:20
n=0;
p(x,y)=0;
if find(BLT_elnino(i,x,y)>=0.9.*BLT_el_avg(x,y) & BLT_elnino(i,x,y)<=(1.1)*BLT_el_avg(x,y));
n=n+1;
p(x,y)=p(x,y)+BLT_elnino(i,x,y);
end
end
end
end composite(x,y)=p(x,y)./n;
1 Comment
Image Analyst
on 13 Dec 2017
Is this your "Answer" to Alexander's 3-year-old original question at the very top of this page?
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!