将所有迭代循环结果存储在矩阵中

时间:2013-10-14 15:46:08

标签: matlab loops matrix iteration

我有一个代码,基本上选择一些数据分段并对这些数据段执行一些计算(回归线,所以我看一下斜率和回归线的截距)。代码工作正常,最后它在命令窗口中显示所有循环迭代的结果,但它只在矩阵(称为final)中进行最后一次循环迭代结果。基本上我想把所有结果都放在一个矩阵中。

我在这里附上代码:

Data = csvread(inputFile);

for i=1:max(L)
try
B = [ones(length(result{i}),1) result{i}(:,1)] \ result{i}(:,2);
catch
    continue 
end


intercept = B(1);
slope  = B(2);
position  = (slope.*result{i}(end,1)+intercept)-(slope .*result{i}(1:1)+intercept);
time = result{i}(end,1)-result{i}(1:1);

final =[slope, intercept, position, time] 

end

目前我在命令窗口中得到每个循环的结果。所以我得到了这样的东西:

final =

   4.6607  -27.7973    0.0621    0.0133


final =

   -0.0647   -0.1299   -0.0517    0.7992


final =

   -9.0676   74.6981   -0.0604    0.0067


final =

   0.3176   -3.2234    0.0698    0.2198


final =

   0.2153   -2.3666    0.0631    0.2930


final =

   -5.1864   45.6418   -0.2072    0.0400


final =

   -1.0881    9.1772   -0.0797    0.0733


final =

   -0.5416    4.3386   -0.1605    0.2964


final =

   -4.4898   39.8786   -1.2409    0.2764


final =

   -3.0985   26.3052   -0.0619    0.0200


final =

   2.0871  -24.2849    0.0208    0.0100


final =

   1.0060  -15.6203    0.0067    0.0067

如何在矩阵中获得所有这些?

提前致谢!!

2 个答案:

答案 0 :(得分:1)

final(i,:) =[slope, intercept, position, time];

for循环之前预先分配您的数组:

final = zeros(max(L),4);

答案 1 :(得分:-1)

2016年帮助了我!!

我的问题很相似,但更简单:这是我的代码以及如何帮助我:

tic
clear all; clc; 

 A = [1 1 1 0 0; 1 0 0 1 0; 0 1 0 0 1]
 b = [1; 1; 1];
[m,n] = size(A)

Puntos_extremos = factorial(n)/(factorial(m)*factorial(n-m));
fprintf('Puntos_extremos = %d\n\n', Puntos_extremos);

permut = [1:n];
P = nchoosek(permut,m);

xx = zeros(Puntos_extremos,n);
for i=1:Puntos_extremos
    B  = A(:,P(i,:));
 if det(B) == 0
    fprintf('x%d^T =',i); fprintf('La submatriz B es singular\n')
 else 

     x_B= B\b;
    x_N = zeros(2,1);
    x = [x_B;x_N];
    fprintf('x%d^T =',i); fprintf('%4d', x'); fprintf('\n');

 end


% Here i got your help. Thanks bot of you!!
 xx(i,:) = x;
end

VectFAct = xx

toc