Rearranging matrix for interpolation and the problem of memory

1 view (last 30 days)
Hi,
I want to interpolate data onto satellite tracks using the following code:
for day = 1:size(ssh_int,1);
mask(:,:,day) = mask50;
ma = permute(mask,[3 1 2]);
%f = ndgrid(ma);
m50m(:,:,day)=interp2(lonrep,latrep,squeeze(mask(:,:,day)),REF_lon,REF_lat,'linear
With lonrep and latrep being the repeated matrices of lon and lat from the mask. Both of which are 3600 x 1682. mask is 3600 x 1682 x 365. REF_lon/lat are 3127 x 254 (satellite index & arcs).
However, I get the following error message:
Error using griddedInterpolant
Data is in MESHGRID format, NDGRID format is required. Convert your data as follows: X = X'; Y = Y'; V = V'; F = griddedInterpolant(X,Y,V)
Error in interp2/makegriddedinterp (line 220) F = griddedInterpolant(varargin{:});
Error in interp2 (line 133) F = makegriddedinterp(X, Y, V, method);
Error in depth_criterion (line 52) m50m(:,:,day)=interp2(lonrep,latrep,squeeze(mask(:,:,day)),REF_lon,REF_lat,'linear');
From another question I asked related to this one, I got an answer that the data should be sorted monotonically.
I have tried usind code like
ndgrid(blahh)
or
meshgrid(blahh)
or
sort(blahh)
but to no provail. I am fairly new to matlab so I still don't understand fully the wording usually used.
Can someone please help me?
also, I keep getting memory error messages. Are there any good tips to get around this? currently, I use t = ones(matrix) method or x NaN's or by deleting large variables after usage. I always get this messages when dealing with 3D matrices...
thanks, Michael
here is a link to my previous question that I asked related to this one:

Accepted Answer

Matt J
Matt J on 8 Jun 2014
Edited: Matt J on 8 Jun 2014
I think it would be easier to use griddedInterpolant as below, as well as more memory-conserving. I assume throughout that latitudes vary row-to-row while longitude varies column-to-column. This appears to be your convention in your posted code. I also assume that it truly is your intention that all mask(:,:,day) are identical to mask50, in which case you are repeating the same interpolation operation for each day unnecessarily. Thus, I have removed the creation of m50m from the loop.
Do not make matrices latrep,lonrep of replicated longitudes and latitudes unless you need them for something else. You certainly don't need them for interpolation. In the following code, I use the original vectors of lats and lons. If REF_lat,REF_lon are replicated in a similar manner, you can avoid that too, but then we need to talk further.
F=griddedInterpolant({lats,lons},mask50);
m50m=repmat(F(REF_lat,REF_lon),1,1,365);
for day = 1:size(ssh_int,1); %reduced for-loop
mask(:,:,day) = mask50;
ma = permute(mask,[3 1 2]); %<---suspicious
end
I'm not sure what the rest of the operations in your for-loop are trying to accomplish, but it looks suspicious. It seems crazy to be permuting a complete 3D array in every iteration of the loop.
  5 Comments
Matt J
Matt J on 9 Jun 2014
I need to put 1,1,365 into [1 1 365].
No idea why,
>> repmat(rand(3),1,1,2)
ans(:,:,1) =
0.9628 0.0710 0.8494
0.7412 0.6020 0.8488
0.8192 0.1095 0.4382
ans(:,:,2) =
0.9628 0.0710 0.8494
0.7412 0.6020 0.8488
0.8192 0.1095 0.4382

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!