matlab 2013 中的散点数据插值

scatter data interpolation in matlab 2013

我想对温度数据中的缺失值或 NaN 值使用散点数据插值?我有文本文件,每个包含三列

  1. 第一列是纬度
  2. 第二列是经度
  3. 第三列包含温度值

温度列有 -9999.000 值,表示缺​​失或 NaN 数据。我想从剩余的已知值中插入这些值。我想使用 F=scatteredInterpolant(x,y,v)

其中x,y为样本点的坐标,v为这些样本点对应的值。在制作 F(q) 之后,其中 q 是查询点。这将代表第 3 列中的 NaN 或温度缺失值。

我已经准备好这段代码,但不知道下一步该怎么做??

metC = {'linear','cubic','next','pchip','previous','spline','v5cubic','nearest'};
    % Read the file data:
S = dir('Output_*.txt');
N = sort({S.name});
nmf = numel(N);
nmr = size(load(N{1},'-ascii'),1);
mat = zeros(nmr,3,nmf);
for k = 1:nmf
    mat(:,:,k) = load(N{k},'-ascii');
end
tmp = 0==diff(mat(:,1:2,:),1,3);
assert(all(tmp(:)),'First columns do not match')
% Rearrange:
[VC,NA,IC] = unique(mat(:,1,1));
[VR,NA,IR] = unique(mat(:,2,1));
out = reshape(mat(:,3,:),numel(VR),numel(VC),nmf);
% Detect -9999:
idx = out<(-9998);

这里我不知道我走对不对??

 Interpolate:
vec = 1:nmf;% 24 times
for m = 1:numel(metC)% the length of techniques let say 3 times in this case
    metS = metC{m} % the method
for r = 1:numel(VR)% 19 times
    for c = 1:numel(VC)% 17 times
          idy = squeeze(idx(r,c,:)).'; % removing singleton dimension. Here idy will be position of the without -9999 value
        if any(idy) %check for -9999
            Xold = vec(~idy); %giving x which are sample points
            Yold = squeeze(out(r,c,~idy)).'; % I DO NOT KNOW WHAT THIS LINE DOING?
            Xnew = vec(idy); % these are query points which we want to interpolate
           out(r,c,idy) = scatteredInterpolant(Xold,Yold,???????????,Xnew,metC,'extrap');
        end
    end
end
end

我的数据集是这样的:

21.500  60.500  295.867
21.500  61.500  295.828
21.500  62.500  295.828
21.500  63.500  295.867
21.500  64.500  296.102
21.500  65.500  296.234
21.500  66.500  296.352
21.500  67.500  296.336
21.500  68.500  296.305
21.500  69.500  298.281
21.500  70.500  301.828
21.500  71.500  302.094
21.500  72.500  299.469
21.500  73.500  301.711
21.500  74.500  -9999.000
21.500  75.500  -9999.000
21.500  76.500  -9999.000
21.500  77.500  -9999.000
21.500  78.500  -9999.000
22.500  60.500  295.477
22.500  61.500  295.484
22.500  62.500  295.516
22.500  63.500  295.547
22.500  64.500  295.852
22.500  65.500  295.859
22.500  66.500  295.852
22.500  67.500  295.711
22.500  68.500  295.969
22.500  69.500  298.562
22.500  70.500  300.828
22.500  71.500  302.352
22.500  72.500  300.570
22.500  73.500  301.383
22.500  74.500  -9999.000
22.500  75.500  -9999.000
22.500  76.500  -9999.000
22.500  77.500  -9999.000
22.500  78.500  -9999.000
23.500  60.500  294.906
23.500  61.500  294.898
23.500  62.500  295.000
23.500  63.500  295.078
23.500  64.500  295.297
23.500  65.500  295.359
23.500  66.500  295.297
23.500  67.500  295.312
23.500  68.500  296.664
23.500  69.500  298.781
23.500  70.500  299.211
23.500  71.500  300.109
23.500  72.500  301.000
23.500  73.500  301.594
23.500  74.500  302.000
23.500  75.500  -9999.000
23.500  76.500  -9999.000
23.500  77.500  -9999.000
23.500  78.500  -9999.000
24.500  60.500  294.578
24.500  61.500  294.516
24.500  62.500  294.734
24.500  63.500  294.789
24.500  64.500  294.844
24.500  65.500  294.562
24.500  66.500  294.734
24.500  67.500  296.367
24.500  68.500  297.438
24.500  69.500  298.531
24.500  70.500  298.453
24.500  71.500  299.195
24.500  72.500  300.062
24.500  73.500  -9999.000
24.500  74.500  -9999.000
24.500  75.500  -9999.000
24.500  76.500  -9999.000
24.500  77.500  -9999.000
24.500  78.500  -9999.000
25.500  60.500  296.258
25.500  61.500  296.391
25.500  62.500  296.672
25.500  63.500  296.398
25.500  64.500  295.773
25.500  65.500  295.812
25.500  66.500  296.609
25.500  67.500  297.977
25.500  68.500  297.109
25.500  69.500  297.828
25.500  70.500  298.430
25.500  71.500  298.836
25.500  72.500  298.703
25.500  73.500  -9999.000
25.500  74.500  -9999.000
25.500  75.500  -9999.000
25.500  76.500  -9999.000
25.500  77.500  -9999.000
25.500  78.500  299.023
26.500  60.500  -9999.000
26.500  61.500  298.266
26.500  62.500  296.773
26.500  63.500  -9999.000
26.500  64.500  -9999.000
26.500  65.500  -9999.000
26.500  66.500  297.250
26.500  67.500  296.188
26.500  68.500  295.938
26.500  69.500  296.906
26.500  70.500  297.828
26.500  71.500  299.312
26.500  72.500  299.359
26.500  73.500  -9999.000
26.500  74.500  -9999.000
26.500  75.500  -9999.000
26.500  76.500  -9999.000
26.500  77.500  298.875
26.500  78.500  296.773
27.500  60.500  -9999.000
27.500  61.500  -9999.000
27.500  62.500  -9999.000
27.500  63.500  -9999.000
27.500  64.500  -9999.000
27.500  65.500  -9999.000
27.500  66.500  -9999.000
27.500  67.500  295.352
27.500  68.500  295.148
27.500  69.500  295.750
27.500  70.500  295.750
27.500  71.500  296.070
27.500  72.500  295.227
27.500  73.500  -9999.000
27.500  74.500  -9999.000
27.500  75.500  -9999.000
27.500  76.500  -9999.000
27.500  77.500  -9999.000
27.500  78.500  296.609
28.500  60.500  -9999.000
28.500  61.500  -9999.000
28.500  62.500  -9999.000
28.500  63.500  -9999.000
28.500  64.500  -9999.000
28.500  65.500  -9999.000
28.500  66.500  -9999.000
28.500  67.500  295.773
28.500  68.500  295.375
28.500  69.500  295.438
28.500  70.500  294.664
28.500  71.500  294.906
28.500  72.500  294.812
28.500  73.500  295.805
28.500  74.500  -9999.000
28.500  75.500  -9999.000
28.500  76.500  -9999.000
28.500  77.500  -9999.000
28.500  78.500  -9999.000
29.500  60.500  -9999.000
29.500  61.500  -9999.000
29.500  62.500  -9999.000
29.500  63.500  -9999.000
29.500  64.500  -9999.000
29.500  65.500  -9999.000
29.500  66.500  -9999.000
29.500  67.500  295.719
29.500  68.500  296.797
29.500  69.500  293.375
29.500  70.500  294.305
29.500  71.500  294.070
29.500  72.500  293.750
29.500  73.500  295.539
29.500  74.500  -9999.000
29.500  75.500  -9999.000
29.500  76.500  -9999.000
29.500  77.500  -9999.000
29.500  78.500  -9999.000
30.500  60.500  -9999.000
30.500  61.500  -9999.000
30.500  62.500  -9999.000
30.500  63.500  -9999.000
30.500  64.500  -9999.000
30.500  65.500  -9999.000
30.500  66.500  -9999.000
30.500  67.500  -9999.000
30.500  68.500  -9999.000
30.500  69.500  -9999.000
30.500  70.500  293.320
30.500  71.500  292.930
30.500  72.500  293.570
30.500  73.500  294.648
30.500  74.500  295.383
30.500  75.500  -9999.000
30.500  76.500  -9999.000
30.500  77.500  -9999.000
30.500  78.500  -9999.000
31.500  60.500  -9999.000
31.500  61.500  -9999.000
31.500  62.500  -9999.000
31.500  63.500  -9999.000
31.500  64.500  -9999.000
31.500  65.500  -9999.000
31.500  66.500  -9999.000
31.500  67.500  -9999.000
31.500  68.500  -9999.000
31.500  69.500  -9999.000
31.500  70.500  293.992
31.500  71.500  293.422
31.500  72.500  294.438
31.500  73.500  294.141
31.500  74.500  -9999.000
31.500  75.500  -9999.000
31.500  76.500  -9999.000
31.500  77.500  -9999.000
31.500  78.500  -9999.000
32.500  60.500  -9999.000
32.500  61.500  -9999.000
32.500  62.500  -9999.000
32.500  63.500  -9999.000
32.500  64.500  -9999.000
32.500  65.500  -9999.000
32.500  66.500  -9999.000
32.500  67.500  -9999.000
32.500  68.500  -9999.000
32.500  69.500  -9999.000
32.500  70.500  -9999.000
32.500  71.500  294.312
32.500  72.500  294.812
32.500  73.500  -9999.000
32.500  74.500  -9999.000
32.500  75.500  -9999.000
32.500  76.500  -9999.000
32.500  77.500  -9999.000
32.500  78.500  -9999.000
33.500  60.500  -9999.000
33.500  61.500  -9999.000
33.500  62.500  -9999.000
33.500  63.500  -9999.000
33.500  64.500  -9999.000
33.500  65.500  -9999.000
33.500  66.500  -9999.000
33.500  67.500  -9999.000
33.500  68.500  -9999.000
33.500  69.500  -9999.000
33.500  70.500  -9999.000
33.500  71.500  -9999.000
33.500  72.500  -9999.000
33.500  73.500  -9999.000
33.500  74.500  -9999.000
33.500  75.500  -9999.000
33.500  76.500  -9999.000
33.500  77.500  -9999.000
33.500  78.500  -9999.000
34.500  60.500  -9999.000
34.500  61.500  -9999.000
34.500  62.500  -9999.000
34.500  63.500  -9999.000
34.500  64.500  -9999.000
34.500  65.500  -9999.000
34.500  66.500  -9999.000
34.500  67.500  -9999.000
34.500  68.500  -9999.000
34.500  69.500  -9999.000
34.500  70.500  -9999.000
34.500  71.500  -9999.000
34.500  72.500  -9999.000
34.500  73.500  -9999.000
34.500  74.500  -9999.000
34.500  75.500  -9999.000
34.500  76.500  -9999.000
34.500  77.500  -9999.000
34.500  78.500  -9999.000
35.500  60.500  -9999.000
35.500  61.500  -9999.000
35.500  62.500  -9999.000
35.500  63.500  -9999.000
35.500  64.500  -9999.000
35.500  65.500  -9999.000
35.500  66.500  -9999.000
35.500  67.500  -9999.000
35.500  68.500  -9999.000
35.500  69.500  -9999.000
35.500  70.500  -9999.000
35.500  71.500  -9999.000
35.500  72.500  -9999.000
35.500  73.500  -9999.000
35.500  74.500  -9999.000
35.500  75.500  -9999.000
35.500  76.500  -9999.000
35.500  77.500  -9999.000
35.500  78.500  -9999.000
36.500  60.500  276.742
36.500  61.500  274.406
36.500  62.500  -9999.000
36.500  63.500  -9999.000
36.500  64.500  -9999.000
36.500  65.500  272.219
36.500  66.500  273.023
36.500  67.500  275.875
36.500  68.500  -9999.000
36.500  69.500  -9999.000
36.500  70.500  -9999.000
36.500  71.500  -9999.000
36.500  72.500  -9999.000
36.500  73.500  -9999.000
36.500  74.500  -9999.000
36.500  75.500  -9999.000
36.500  76.500  -9999.000
36.500  77.500  -9999.000
36.500  78.500  -9999.000
37.500  60.500  277.406
37.500  61.500  277.547
37.500  62.500  276.375
37.500  63.500  275.484
37.500  64.500  276.820
37.500  65.500  275.312
37.500  66.500  274.875
37.500  67.500  275.875
37.500  68.500  -9999.000
37.500  69.500  -9999.000
37.500  70.500  -9999.000
37.500  71.500  -9999.000
37.500  72.500  -9999.000
37.500  73.500  -9999.000
37.500  74.500  -9999.000
37.500  75.500  -9999.000
37.500  76.500  -9999.000
37.500  77.500  -9999.000
37.500  78.500  -9999.000

这是一个使用 scatteredInterpolant 的例子。

% Load the data
data = load('temperature_data.txt');
% separate the data columns, just to make the code clear
Lat = data(:,1); % Column 1 is Latitude
Lon = data(:,2); % Column 2 is Longitude
Tmp = data(:,3); % Column 3 is Temperature
% Find the "good" data points
good_temp = find(Tmp > -9999);
% create the interpolant object using the good data points
T = scatteredInterpolant(Lat(good_temp),Lon(good_temp),Tmp(good_temp),'linear');
% find the "bad" data points
bad_temp = find(Tmp == -9999);
% use the interpolation object to interpolate temperature values
interp_values = T(Lat(bad_temp),Lon(bad_temp));
% replace the bad values with the interpolated values
Tmp(bad_temp) = interp_values;