如何将循环结果存储在矩阵中?

时间:2014-12-15 12:13:34

标签: matlab simulation regression

我有以下循环

x = [1 2 3 4 5;4 5 6 8 9;8 7 6 3 1;5 6 7 9 1;6 4 2 9 6]

y=[10 30 24 35 40]'
one=[]
for i=1:5
    a=i;
    ind=[a]
    one=x(:,[i])
[b_LS, sigma_b_LS, s_LS] = lscov(one,y)
s = s_LS
aicx1=size(one,1)*log(s)+2*size(one,2)
end

我想将结果存储为:

A=[ind;aicx1]例如A=[1 2 3 4 5; 26 34 24 325]

1 个答案:

答案 0 :(得分:1)

你可以在循环结束时添加:

x = [1 2 3 4 5; 4 5 6 8 9; 8 7 6 3 1; 5 6 7 9 1; 6 4 2 9 6];
y = [10 30 24 35 40]';
one=[];

for ii=1:5
    one = x(:,ii);
    [b_LS, sigma_b_LS, s_LS] = lscov(one,y);
    s = s_LS;
    aicx1 = size(one,1) * log(s) + 2 * size(one,2);

    %% Add this
    A(1,ii) = ii;
    A(2,ii) = aicx1;
end

备注

避免使用ij作为变量,因为它们用于复数

如果您不希望/需要值显示在命令窗口中,请在句子末尾添加;