2013-08-14 2 views
0

for 루프를 사용하여 셀 배열에서 일부 계산을 시도하지만 마지막 루프의 결과 만 표시됩니다. Matlab에서 모든 루프에 대한 결과를 표시하고 싶습니다.출력 반복은 Matlab의 루프에서 발생합니다.

기울기 =

4.4089 

시간 =

0.5794 

위치 =

을 : 내가 무엇을 얻을입니다

slope=[]; 
time=[]; 
position= []; 


for p=1:max(L) % max L gives the number of result{n}. so if max(L)=6 we have from result{1} to result{6} and therefore 6 final values that i want to get% 
    a=result{n}(:,1); 
    b=result{n}(:,2); 
end 


B = [ones(length(a),1) a] \ b % this is to obtain the slope and intercept of a lin. regresion 

slope = B(2) 

time = result{n}(end,1)-result{n}(1:1) 
position = (slope.*result{n}(end,1)+intercept)-(slope.*result{n}(1:1)+intercept) 

출력에 순간 : 여기에 코드가있다

2.5546 

이 결과는 정확합니다. 그러나이 값은 결과 {6}로 얻은 값이므로 이전 값이 필요합니다.

도움을 주시면 감사하겠습니다.

미리 감사드립니다.

답변

1

인덱스가 엉망입니다. 코드에서 수행 한 작업을 이해하는 데 약간의 어려움이 있습니다. 그러나 코드는 result이 선언되지 않았으므로 다음과 유사합니다 (의사 코드 :

).
slope=zeros(1,max(L)); % Pre allocate zeros, one index for each interation 
time=zeros(1,max(L)); 
position=zeros(1,max(L)); 
a=zeros(1,max(L)); 
b=zeros(1,max(L)); 

for p=1:max(L) % max L gives the number of result{n}. so if max(L)=6 we have from result{1} to result{6} and therefore 6 final values that i want to get% 
    a(p)=result{p}(:,1); 
    b(p)=result{p}(:,2); 
    B = [ones(length(a(p),1) a(p)] \ b(p) % this is to obtain the slope and intercept of a lin. regresion 
    slope(p) = B(2) 
    time(p) = result{p}(end,1)-result{p}(1:1) 
    position(p) = (slope(p).*result{p}(end,1)+intercept)-(slope (p) .*result{p}(1)+intercept) 
end 

position (6)은 이전 값 (5)을 얻습니다.

1

가장 쉬운 방법은 ";"을 제거하는 것입니다. 명령 창에 인쇄 할 행이있는 경우 그러면 필요한 모든 루프 값이 표시됩니다.

for p=1:max(L) 
a=result{n}(:,1) 
b=result{n}(:,2) 
end 
0

루프 내에서 모든 계산을 수행 할 수 있으며 ";" 대신. 루프에서 나온 후에 결과를 얻으면 마지막 것만 가져옵니다.