【问题标题】:How to find the minimum sum and the corresponding index by iterations?如何通过迭代找到最小和和对应的索引?
【发布时间】:2020-09-12 23:08:58
【问题描述】:

我需要根据将时间序列分为两部分的方程来确定最小值和达到时对应的指数:

在这个等式中,n1n2 是时间序列的两部分的大小,它们构成大小为n 的原始时间序列。为等式设置以下附加条件:时间序列第一部分的最小大小设置为n1 = 10,其中n2 = n – n1,然后增加大小:n1 = 11, 12,…, k,其中k = n – 10n2 = 10.

我试图编写这个方程以通过迭代找到最小总和,但我不确定脚本是否正确。特别是,循环是否正确应用?另外,这里最后似乎是n2=11,但迭代结束时它必须是10

n=66; % The size of the original time series for the characteristic Y (e.g., temperature)
n1=n-20; % This operation is needed for the loops below
minsum=10000000; % Declaration of the variable needed for the "if" operator at the end of the script.
for j=1:n1
    ik=10+j-1;  % This is from 10 to 55
    s31=0; % This is preallocation
    for i=1:ik  % This is from 1 to 10, 11,...,55
        s31=s31+Y(i); % The values are taken from 1 to 10,11,...,55
        cn=ik; % This is from 10 to 55
        Mean1=s31/cn; % Finding of the means from 1 to 10,11,...,55. So, the minimum size of the sample over which the mean is found is 10 years
        s32=0;
        s32=s32+(Y(i)-Mean1).^2; % This is the first term of the equation
        s41=0;
        in1=ik+1; % This is from 11 to 56
        for k=in1:n % This is from 11 to 66
            s41=s41+Y(k);
            mn=n-in1+1; % This is from 56 to 11
            Mean2=s41/mn; % Finding of the means from 66 to 11,12,...,56 or from 11,12,...,56 to 66 to be consistent with the notation in the formula. So, the minimum size of the sample over which the mean is found is 11 years
            s42=0;
            s42=s42+(Y(k)-Mean2).^2; % This is the second term of the equation
            summation=s32+s42; % Finding the sums between all possible sizes of the two parts of the time series
            if summation<minsum
                minsum=summation % The minimum sum is displayed in the last output among the iterated values.
                imin=in1 % Finding the index in the original time series for which the minimum sum is achieved.
            end
        end
    end
end

【问题讨论】:

    标签: matlab for-loop if-statement indexing min


    【解决方案1】:

    这个任务实际上更简单,需要更短的代码。假设我们有一系列年份的月度温度数据。那么,使用两个'for'循环就足够解决问题了:

    a=load('filename.txt');
    JanT=a(:,2); % Monthy temperature data for January
    Years=a(:,1); % Years in the time series
    n=length(JanT); % The size of the original time series
    n1=n-19; % This operation is needed for the 'for' loop below
    n2=n-9; % This operation is needed for the 'for' loop below
    
    S1=[]; % Initialisation of the sum for the first term of the equation (in the vector form) 
    for i=1:n1
        A=sum((JanT(1:i+9)-mean(JanT(1:i+9))).^2); % Finding of the sums from 1 to 10, 11,...56. 
    S1=[S1,A]; % Writing of the obtained results into the array
    end
    
    S2=[]; % Initialisation of the sum for the second term of the equation (in the vector form)
    for j=11:n2
        B=sum((JanT(j:n)-mean(JanT(j:n))).^2); % Finding of the sums from 66 to 11, 12,...57
    S2=[S2,B]; % Writing of the obtained results into the array
    end          
    
    Sum=S1+S2; % Finding the sums between all possible sizes of the two parts of the time series
    
    [minsum,imin]=min(Sum) % Finding the minimum sum and the corresponding index when it is achieved
    Years=Years+10; % Shifting the years properly to find the year when the minimum sum is achieved
    imin=Years(imin) % Attribution of the found index to the year in the original time series
    

    【讨论】:

    • 你应该预先分配你的数组:而不是S1=[],做S1=zeros(n1,1),然后在循环中做S1(i)=sum(...)。这避免了在每次循环迭代中分配数组和复制数据。您的 MATLAB 编辑器应该使用 S1 变量下的红色波浪线警告您这一点;如果您将鼠标悬停在它上面,您将看到有关什么是不良做法以及如何改进它的信息。
    • @CrisLuengo 谢谢你的建议。是的,我同意这种方法效率更高。我看到了警告,但忽略了它。但是,对于第二个循环,S2 的第一个10 值在此方法中为零,因此,对于Sum 变量,必须使用Sum=S1+S2(11:end)。这可行,但看起来很不自然。
    猜你喜欢
    • 2017-11-28
    • 2018-08-12
    • 2011-07-23
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多