필터 지우기
필터 지우기

simplyfing computations in arrays

조회 수: 1 (최근 30일)
Sara
Sara 2012년 7월 4일
int = zeros(1,length(x));
for n = 3:length(x)
y1 = x(2:n) ;
t1 = n*dt - (2*dt:dt:n*dt) ;
y2 = x(1:n-1) ;
t2 = n*dt - (dt:dt:(n-1)*dt) ;
int(n) = sum (t1.^(k-1)/factorial(k-1).*y1 ...
+ t2.^(k-1)/factorial(k-1).*y2)*dt/2 ;
end
I want to write t1= ((n-2)*dt:dt:0dt) is it okay? and also t2=((n-1)dt:dt:dt).
Is it possible? I sew also some errors...

채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 4일
편집: Walter Roberson 2012년 7월 4일
Is t1= ((n-2)*dt:dt:0dt) intended to go to 0 ? If so then because n-2 is positive so you are going backwards from positive towards 0, you will need
(n-2)*dt: -dt : 0
  댓글 수: 2
Sara
Sara 2012년 7월 4일
Walter, Do you have any suggestion, how can I replace 'for' loop in this code to another data type e.g. vector? (New to MATLAB) Thanks in Advance
Walter Roberson
Walter Roberson 2012년 7월 5일
You cannot replace the "for" loop with a different data structure. Your loop index, "n", is used as the endpoint for a colon index, so you are using expressions with different numbers of terms for different values of "n", and expressions with differing numbers of terms depending on the loop variable, cannot be vectorized.
To have any hope of vectorization, you will need to figure out the symbolic difference between the results for adjacent values of "n", and hope that that difference consists only of constants and individual array elements.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

F.
F. 2012년 7월 4일
Hello,
t1 = ( (n-2):-1:0 ) *dt ;
t2 = t1 + dt ;
no ???
  댓글 수: 1
Sara
Sara 2012년 7월 4일
편집: Sara 2012년 7월 5일
for both lines , your replacements for t1 and t2 are fine

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by