-1

I have a code that basically selects some data segements and performs some calculations on these data segments (regression lines, so I look at the slope and the intercept of the resgression lines). The code works fine and at the end it displays in the Command window the results of all the loop iterations but it only sotres in a matrix (called final) the last loop iteration results. Basically I would like to have all the results in a matrix.

Here i attach the code:

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

At the moment I get the result of each loop in the command window. So i obtain something like this:

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

How can I get all this in a matrix?

Thanks in advance!!

4

2 回答 2

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

for在循环之前预分配你的数组:

final = zeros(max(L),4);
于 2013-10-14T15:53:23.740 回答
-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
于 2016-04-13T21:39:26.150 回答