2016-09-18 118 views
0

早上好,保存所有循环结果

我想我的问题一定很简单,但我不知道该怎么做。我有一个循环,我想'保存'结果。问题出在代码我只能保存'最后'列,而不是全部。代码如下:

b = xlsread('Data.xls', 'Sheet1'); %here I'm reading the excel data 
d= size(b); % the size of the input table is 8 x 16 columns. 
cols= d(:,2); 
Results=[] 

for a=b(:,2:cols) 

    n= 2; 
    m2 = ar(a,n); 
    K=6; 
    hf2=forecast(m2,a,K); 

for a=b(:,2:cols) % here I try to save the results but it only save the 'last column', not all the columns. 
    Results=[forecast(m2,a,K)] 
    end 
end 

可能是什么问题?

在此先感谢! :)

回答

0

问题是第二个循环,您在哪里保存输出。在每个迭代中用替换为新输出。您应该在循环之前将Results预先分配给其最终大小,并在循环中逐列填充它。有两个循环也没有意义。在这里,我摆脱了一些额外的变量,并试图使您的代码整齐:

b = xlsread('Data.xls', 'Sheet1'); 
[~, cols] = size(b); 
n = 2; 
K = 6; 
Results = zeros(K, cols - 1); 
for ii = 2:cols 
    a = b(:, ii); 
    Results(: , ii) = forecast(ar(a, n), a, K); 
end