redefine array in for loop

Hi Guys,
for n = 3:length(x)
y1 = x(2:n) ;
t1 = ((n-2):-1:0)*dt ;
% y2 = x(1:n-1) ;
y2 = y1 - 1 ;
t2 = t1 +dt;
int(n) = sum (t1.^(k-1)/factorial(k-1).*y1 + t2.^(k-1)/factorial(k-1).*y2)*dt/2 ;
end
I want to define y2= y1 -1 instead y2= x (1:n-1) which I have commented above. By y2 = y1 - 1, occurred some errors in MATLAB. Why? It is not correct?

댓글 수: 1

Walter Roberson
Walter Roberson 2012년 7월 6일
What error did you encounter with the code you tried?

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

답변 (1개)

John Petersen
John Petersen 2012년 7월 5일

0 개 추천

y1-1 means the vector y1 with the quantity 1 subtracted from it. The commented code shifts the elements by one element. Which do you want to do?

댓글 수: 5

Sara
Sara 2012년 7월 5일
I intend instead of defining the variable y2 with x(1:n-1) , define it by y1 -1 . Maybe I can omit the y2 variable.
F.
F. 2012년 7월 5일
could you give us an exemple of x ??? I'm ok with John y2 = x(1:n-1) and y2 = y1 - 1 , it's not the same thing !!!
John Petersen
John Petersen 2012년 7월 5일
I think you want to shift the elements by 1, but you want to define it your own way. If you want to use Matlab, you need to use it's syntax.
Sara
Sara 2012년 7월 11일
int = zeros(1,length(x));
if k <= 0
int = x ;
return
end
for n = 3:length(x)
y1 = x(2:n) ;
t1 = ((n-2):-1:0)*dt ;
y2 = x(1:n-1) ;
% y2 = -1 +y1 ;
t2 = t1 +dt;
int(n) = sum (t1.^(k-1)/factorial(k-1).*y1 + t2.^(k-1)/factorial(k-1).*y2)*dt/2 ;
end
I mean, is there anyway to define y2 from y1? And not from variable x? Could I omit the variable y2 from the code ?
F.
F. 2012년 7월 11일
If you don't use y1 and y2 after...
if k <= 0
int = x ;
return
else
int = zeros(1,numel(x));
end
for n = 3:numel(x)
t1 = ((n-2):-1:0)*dt ;
t2 = t1 +dt;
int(n) = sum ( ...
t1.^(k-1)/factorial(k-1) .* x( 2 : n ) + ...
t2.^(k-1)/factorial(k-1) .* x( 1 : n-1 ) ...
)*dt/2 ;
end

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2012년 7월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by