2013-10-14 73 views
-1

我有一段代碼,它基本上選擇一些數據段並對這些數據段執行一些計算(迴歸線,所以我看一下延伸線的斜率和截距)。代碼工作正常,最後它在Command窗口中顯示所有循環迭代的結果,但它只能在最後一個循環迭代結果的矩陣(稱爲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 

我怎樣才能得到所有這一切矩陣?

在此先感謝!

+0

當顯示大量MATLAB命令窗口輸出的,最好是用'格式compact':HTTP:// www.mathworks.com/help/matlab/ref/format.html#inputarg_Style – chappjc

回答

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

預分配for循環之前您的數組:

final = zeros(max(L),4); 
-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 
+0

歡迎來到StackOverflow。除了提供代碼之外,還要確保包含一些文字,說明您的代碼有助於回答OP的內容和原因。 – buczek