Rearranging matrix for interpolation and the problem of memory
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Matt J
2014 年 6 月 8 日
編集済み: Matt J
2014 年 6 月 8 日
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 件のコメント
Matt J
2014 年 6 月 9 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Multidimensional Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!