Cendrilla Helmy
Cendrilla Helmy

Reputation: 69

How can i store results from a loop in a matrix?

I have the following loop

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

I want to store result as :

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

Upvotes: 2

Views: 78

Answers (1)

ironzionlion
ironzionlion

Reputation: 899

You could add at the end of the loop:

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

Notes

Avoid using i or j as variables since they are used for complex numbers

Add an ; at the end of the sentence if you don't want/need the values to appear in the command window

Upvotes: 1

Related Questions