Index in position 2 exceeds array bounds (must not exceed 101)

2 views (last 30 days)
I would appreciate any help on this. Problem is defining 'newsst', i have tried with also with a loop (commented) but theres something dimension wise i dont get..
Have attached the data and hope the code is descriptive enough.
% load and store netcdf file variables in separate matrices
ncdisp('sst_2015.nc')
sst = ncread('sst_2015.nc','sea_surface_temperature');
time = ncread('sst_2015.nc','time');
lat = ncread('sst_2015.nc','lat'); %latitude
lon = ncread('sst_2015.nc','lon'); %longitude
%exclude potential values from outside the defined box of coordinates below
% Note: use of '&&' instead of '&' below creates an error: 'Operands to the || and &&
% operators must be convertible to logical scalar values'. Works with '&',
% not sure if correct.
newlat = find(lat>50&lat<60);
newlon = find(lon>-3&lon<11);
%change to desired degree of resolution for latitude and longitude
ds_lat=0.1000;
ds_lon = 0.1666;
lati=50:ds_lat:60;
loni=-3:ds_lon:11;
[Loni,Lati] = meshgrid(loni,lati) ;
newsst = sst(newlat,newlon,time);
% newsst = zeros (i,j,k); i,j,k loop base on dimensions of sst
% for i=1:140
% for j=1:101
% for k=1:355
%
% newsst = sst(newlat(i),newlon(j),time(k));
%
% end
%
% end
% end
%interpolate and put everything in a new grid. we will now have values on same
%lat and lon points defined by Loni and Lati
NEWSST = griddata (Loni,Lati,newlat,newlon,newsst);

Accepted Answer

Walter Roberson
Walter Roberson on 18 Mar 2019
netcdf files return arrays with rows and columns exchange relative to what MATLAB uses. You need to index sst(newlon, newlat, time_index)
Do not use the time as an index: it is in POSIX time format, seconds since 1970, values over 1 billion, covering Jan 1, 2004 00:00 to Dec 30, 2004 00:00 .
  5 Comments
Walter Roberson
Walter Roberson on 18 Mar 2019
Code attached.
The interpolation is a bit slow.
Note that the output will be 101 x 85 x 335 .
Note that interpolation for any one time might be affected by earlier and later times. This is a 3D interpolation, not one independent 2D interpolation for each time.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 18 Mar 2019
My guess is that you want all the times, in which case:
newsst = sst(newlat, newlon, :);
Using time as an index into newsst makes no sense.
Note that you don't need find, it's just a waste of time. You can use the logical array directly to index sst:
newlat = lat>50 & lat<60; %find not required. Just extra processing for no reason
newlon = lon>-3 & lon<11; %same
newsst = sst(newlat, newlon, :);
  2 Comments
Denk
Denk on 18 Mar 2019
Well i have followed your advice and now
newsst = sst(newlat,newlon,:);
returns with error message 'The logical indices in position 2 contain a true value outside of the array bounds'
Denk
Denk on 18 Mar 2019
have changed it to
newsst = sst(newlon,newlat,:);
instead and it works, but then the
NEWSST = griddata (Loni,Lati,newlon,newlat,newsst);
line is bugging with 'invalid input arguments' (error(message('MATLAB:griddata:InvalidInputArgs'));)

Sign in to comment.

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!