RMS 误差、AME、来自文本文件的相关性以及在 matlab 中 excel 中导出夏季统计数据

RMS error, AME, correlation from text files and exporting summery statistics in excel in matlab

尊敬的专家;

我正在尝试编写一个代码,该代码能够计算名称为 output_00.txt 到 [=] 的 24 个文本文件的 RMS、AME、相关性 47=] 在文件夹中。阅读每个文件后,我将每个文本文件中的样本 30%(总样本为 323)设为 -9999.0 然后使用 分散数据插值,我插值这些-9999.0值并交换RMS误差、AME、相关性。

更新 2

    S = dir('interpot_linear_*.txt');
N = sort({S.name});

for K = 1 : length(N)
  infile = N{K};
  whichfile = sscanf(infile, 'interpot_linear_%c%c');
% Load the data
data = load(infile);
  % 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
     % Creating 30% of sample as -9999.0
     n=size(data,1);
     %n=323;
    nr=round(n*0.3);
    idx30=randperm(n,nr);
    Tmp(idx30)=-9999.0;
     % Find the "good" data points
    good_temp = find(Tmp > -9999.000);
     % find the "bad" data points
    bad_temp = find(Tmp == -9999.000);
      % creating vector 
      T = scatteredInterpolant(Lat(good_temp), Lon(good_temp), Tmp(good_temp), 'linear');
    % 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;
      %recall oringinal temperature column
    Tmp_original=data(:,3);
    % Here calculate RMS error
   RMS=sqrt(sum((Tmp_original(:)-Tmp(:)).^2)/n);
    % Here calculate mean absolute error
    MAE=sum(abs(Tmp_original(:)-Tmp(:)))/n;
    % Here calculate correlation coefficient
    correl=corr(Tmp_original(:),Tmp(:));
    %here i am spliting R^2 as two different terms
    M_org= mean(Tmp_original); % mean of original temperature data.
    M_ext= mean(Tmp); % mean of extimated temperature data.
    std_org=std(Tmp_original); % standard deviation of orginal temp
    std_ext=std(Tmp); % standard deviation of extimated temp.
    total=length(Tmp_original);
    M1=(Tmp_original(:)- M_org);
    M2=(Tmp(:)- M_ext);
    M1=M1';
    % Here i am creating numminator of the formula
    Nu=M1*M2;  %giveing sum (M1*M2) 
    % Here i am creating dinominator of the formula
    div=total*std_org*std_ext;
    %R ^2
    R=(Nu/div)^2;
    rep(K,1)=RMS; rep(K,2)=MAE; rep(K,3)=correl; rep(K,4)=R; rep(K,5)=str2num(whichfile);
end
  xlswrite('statistics',rep);

我想在 excel/text 包含 的文件中 导出这 24 个 txt 文件的摘要统计信息四列 即第一列是文件号,第二列是无 RMS 误差,第三列是 AME,第四列是这个文件的相关值?

我的一个文本文件如下所示

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  302.317 
21.500  75.500  302.757 
21.500  76.500  303.030 
21.500  77.500  303.137 
21.500  78.500  303.078 
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  301.311 
22.500  75.500  301.381 
22.500  76.500  301.692 
22.500  77.500  301.837 
22.500  78.500  301.814 
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  300.911 
23.500  76.500  300.520 
23.500  77.500  300.702 
23.500  78.500  300.718 
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  300.351 
24.500  74.500  301.055 
24.500  75.500  300.958 
24.500  76.500  300.512 
24.500  77.500  299.734 
24.500  78.500  299.787 
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  300.207 
25.500  74.500  300.110 
25.500  75.500  300.013 
25.500  76.500  299.917 
25.500  77.500  299.470 
25.500  78.500  299.023 
26.500  60.500  294.484 
26.500  61.500  298.266 
26.500  62.500  296.773 
26.500  63.500  296.892 
26.500  64.500  297.012 
26.500  65.500  297.131 
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  299.262 
26.500  74.500  299.165 
26.500  75.500  299.069 
26.500  76.500  298.972 
26.500  77.500  298.875 
26.500  78.500  296.773 
27.500  60.500  292.710 
27.500  61.500  295.880 
27.500  62.500  294.643 
27.500  63.500  295.710 
27.500  64.500  296.362 
27.500  65.500  296.333 
27.500  66.500  296.452 
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  297.534 
27.500  74.500  297.437 
27.500  75.500  297.340 
27.500  76.500  297.870 
27.500  77.500  297.405 
27.500  78.500  296.609 
28.500  60.500  290.935 
28.500  61.500  293.494 
28.500  62.500  292.513 
28.500  63.500  293.580 
28.500  64.500  294.647 
28.500  65.500  295.715 
28.500  66.500  295.951 
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  296.335 
28.500  75.500  296.864 
28.500  76.500  296.731 
28.500  77.500  294.557 
28.500  78.500  294.696 
29.500  60.500  289.161 
29.500  61.500  291.108 
29.500  62.500  290.383 
29.500  63.500  291.450 
29.500  64.500  292.517 
29.500  65.500  293.585 
29.500  66.500  294.652 
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  295.859 
29.500  75.500  296.057 
29.500  76.500  292.532 
29.500  77.500  292.799 
29.500  78.500  292.924 
30.500  60.500  287.387 
30.500  61.500  288.722 
30.500  62.500  288.253 
30.500  63.500  289.320 
30.500  64.500  290.387 
30.500  65.500  291.455 
30.500  66.500  292.522 
30.500  67.500  292.884 
30.500  68.500  294.198 
30.500  69.500  295.394 
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  290.535 
30.500  76.500  290.929 
30.500  77.500  291.182 
30.500  78.500  291.294 
31.500  60.500  285.613 
31.500  61.500  286.336 
31.500  62.500  286.123 
31.500  63.500  287.190 
31.500  64.500  288.257 
31.500  65.500  289.325 
31.500  66.500  289.239 
31.500  67.500  290.049 
31.500  68.500  291.364 
31.500  69.500  292.678 
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  288.564 
31.500  75.500  289.087 
31.500  76.500  289.468 
31.500  77.500  289.707 
31.500  78.500  289.805 
32.500  60.500  283.839 
32.500  61.500  283.950 
32.500  62.500  283.993 
32.500  63.500  285.060 
32.500  64.500  286.127 
32.500  65.500  286.272 
32.500  66.500  285.726 
32.500  67.500  287.214 
32.500  68.500  288.529 
32.500  69.500  289.843 
32.500  70.500  291.847 
32.500  71.500  294.312 
32.500  72.500  294.812 
32.500  73.500  286.621 
32.500  74.500  287.271 
32.500  75.500  287.781 
32.500  76.500  288.148 
32.500  77.500  288.374 
32.500  78.500  288.458 
33.500  60.500  282.065 
33.500  61.500  281.564 
33.500  62.500  281.863 
33.500  63.500  282.930 
33.500  64.500  283.306 
33.500  65.500  282.759 
33.500  66.500  282.405 
33.500  67.500  284.380 
33.500  68.500  285.694 
33.500  69.500  287.238 
33.500  70.500  289.703 
33.500  71.500  291.025 
33.500  72.500  284.704 
33.500  73.500  285.483 
33.500  74.500  286.120 
33.500  75.500  286.616 
33.500  76.500  286.970 
33.500  77.500  287.182 
33.500  78.500  287.253 
34.500  60.500  280.290 
34.500  61.500  279.178 
34.500  62.500  279.733 
34.500  63.500  280.339 
34.500  64.500  279.792 
34.500  65.500  279.246 
34.500  66.500  279.278 
34.500  67.500  281.545 
34.500  68.500  282.859 
34.500  69.500  285.093 
34.500  70.500  287.237 
34.500  71.500  282.814 
34.500  72.500  283.722 
34.500  73.500  284.487 
34.500  74.500  285.111 
34.500  75.500  285.593 
34.500  76.500  285.933 
34.500  77.500  286.132 
34.500  78.500  286.189 
35.500  60.500  278.516 
35.500  61.500  276.792 
35.500  62.500  277.373 
35.500  63.500  276.826 
35.500  64.500  276.279 
35.500  65.500  275.732 
35.500  66.500  276.150 
35.500  67.500  278.710 
35.500  68.500  280.484 
35.500  69.500  283.450 
35.500  70.500  280.952 
35.500  71.500  281.987 
35.500  72.500  282.881 
35.500  73.500  283.633 
35.500  74.500  284.243 
35.500  75.500  284.712 
35.500  76.500  285.039 
35.500  77.500  285.224 
35.500  78.500  285.267 
36.500  60.500  276.742 
36.500  61.500  274.406 
36.500  62.500  273.859 
36.500  63.500  273.313 
36.500  64.500  272.766 
36.500  65.500  272.219 
36.500  66.500  273.023 
36.500  67.500  275.875 
36.500  68.500  279.662 
36.500  69.500  279.117 
36.500  70.500  280.280 
36.500  71.500  281.302 
36.500  72.500  282.182 
36.500  73.500  282.920 
36.500  74.500  283.517 
36.500  75.500  283.972 
36.500  76.500  284.285 
36.500  77.500  284.457 
36.500  78.500  284.487 
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  277.308 
37.500  69.500  278.600 
37.500  70.500  279.750 
37.500  71.500  280.758 
37.500  72.500  281.624 
37.500  73.500  282.349 
37.500  74.500  282.932 
37.500  75.500  283.374 
37.500  76.500  283.674 
37.500  77.500  283.832 
37.500  78.500  283.849

所以你的问题是,在一次迭代中,加载并分配给数据的文件(在你的代码的第 8 行)是空的,因此数据的大小是 [0,0]。

我假设有一个空文件,这就是您收到此响应的原因。

所以尝试找到那个文件,最好的方法是 运行 脚本,看看你得到错误的 K 值,然后导航到文本文件,看看它是否真的是空的。

您的代码中存在几个问题:

  1. 如果您的文件名是 output_00.txt(例如它们以小写 "o" 开头),则您必须将第一行代码更改为 S = dir('output_*.txt');。 Matlab 区分大小写。如果您的文件名以小写 "o" 开头,那么 Matlab 将永远找不到名称以 "Output"(大写 "O")开头的文件。这可能会解决 data 中的错误。同样的文件名问题会影响到sscanf(),所以同样在sscanf()中你必须要有小写的"o".
  2. RMSE 公式不正确:您不能将两项都平方然后减去它们,您必须反过来做;也就是先减后升幂。正确的公式是RMS=sqrt(sum((Tmp_original(:)-Tmp(:)).^2)/n);
  3. 变量RMScorrelMAE是标量,所以执行(:)运算符是没有意义的,它用于将变量展开为列向量
  4. rep 被声明为 24x3 矩阵,但随后使用行 rep=[RMS(:);MAE(:);correl(:)]; 使 rep 成为 3x1 向量。
  5. 在最后的 for 循环中有几个问题:xlmwrite() 中的第一个参数(例如 Statistics)必须是字符串,而不是变量(除非这样的变量包含文件名作为一个字符串)。第二个参数必须替换为 rep.
  6. TmpTmp_original 都是列向量,所以做 (:) 运算符是没有意义的。见上文。

由于您主要关心的是关于保存到磁盘的问题,所以我将专注于此。我的建议是构建 rep 作为矩阵,然后在扫描和处理所有文件后将此类矩阵保存在 for 循环之外。正如您现在的代码一样,不清楚您是希望 rep 成为矩阵并立即保存到磁盘,还是希望 rep 成为向量并保存到文件中的磁盘-文件时尚。

  1. 然后要做的第一件事就是在 for 循环开始之前一直向上移动 rep=zeros(24,3);。如果您还想附加文件 ID,那么 rep 实际上应该是一个 24x4 矩阵。
  2. rep=[RMS(:);MAE(:);correl(:)]; 更改为 rep(k,1)=RMS; rep(k,2)=MAE; rep(k,3)=correl;。以这种方式,您在第 k 行第 1 列写 RMS,然后在第 k 行第 2 列写 MAE,同样的故事也适用于 correl。随着 k 的进行,这将填充矩阵 rep。如果您还想附加文件 ID,请记住 whichfile 是一个字符串,您不能将字符串和数字连接到同一个矩阵中。由于 str2num(),直接的选择是将 whichfile 从字符串转换为数字,但请记住,这种转换将删除前导零(Matlab 不允许前导零),因此文件 ID 00 将是 0,文件 01 将是 1 等等。所以你可能想添加 rep(k,4)=str2num(whichfile); 并且这也将填充 rep.
  3. 中的第 4 列
  4. 删除内部 for 循环,即包含 xlmwrite() 的循环。我们将以批处理模式写入整个 rep 矩阵,而不是为每个文件写入一行。
  5. 在主 for 循环之后,按如下方式放置 xlmwrite()xlmwrite('myFinalFile',rep); 如果一切正常,myFinalFile 应该包含您的 rep 矩阵作为 24x3 矩阵.您也可以将 myFinalFile 更改为您喜欢的文件名。