simple loop option to reduce code

조회 수: 5 (최근 30일)
Rashmil Dahanayake
Rashmil Dahanayake 2013년 12월 10일
편집: Andrei Bobrov 2013년 12월 10일
Hi, Can anyone suggest a neat way to simply the following code.
T=0.25;
t1=-3*T/12:.001:T/12;
t2=t1(end):.001: t1(end) + T/3;
t3=t2(end):.001: t2(end) + T/3;
t4=t3(end):.001: t3(end) + T/3;
t5=t4(end):.001: t4(end) + T/3;
t6=t5(end):.001: t5(end) + T/3;
---------------------------------------------------
I'm intending to use a for loop as follow but, I'm getting an error message
for r=2:6
t(r)=t(r-1)(end):t1(end):.001: t(r-1)(end) + T/3;
end

채택된 답변

kei hin
kei hin 2013년 12월 10일
T=0.25;
t{1}=-3*T/12:.001:T/12;
for r=2:6
t{r} = t{r-1}(end):.001: t{r-1}(end) + T/3;
end
  댓글 수: 1
Rashmil Dahanayake
Rashmil Dahanayake 2013년 12월 10일
Thanks. ^Cell array option is more concise

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

추가 답변 (2개)

Rashmil Dahanayake
Rashmil Dahanayake 2013년 12월 10일
편집: Rashmil Dahanayake 2013년 12월 10일
figured it out
T=0.25;
t1=-3*T/12:.001:T/12;
Time(1,:)=t1;
for r=2:6
Time(r,:)=Time((r-1),end):.001: Time((r-1),end) + T/3;
end

Andrei Bobrov
Andrei Bobrov 2013년 12월 10일
편집: Andrei Bobrov 2013년 12월 10일
T = .25;
n = 6;
tt = -3*T/12:.001:T/12;
Time1 = bsxfun(@plus,tt,diff(tt([1,end]))*(0:n-1)');

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by