How to write a function with a for loop

조회 수: 4 (최근 30일)
TRISHITA BANERJEE
TRISHITA BANERJEE 2018년 7월 4일
댓글: TRISHITA BANERJEE 2018년 7월 23일
if t(i)=L1*(i-1)+ L[(i-1)/e]*t_k
i get
t1=0;
t2=L1;
t3=2*L1+t_k;
t4=3*L1+t_k;
where e=2 and i want to have a floor function for L[ (i-1)/e].How to create a function for the following with input e,imax,L1 and t_k like function delay(e,imax,L1,t_k) with a for loop
  댓글 수: 2
TRISHITA BANERJEE
TRISHITA BANERJEE 2018년 7월 4일
편집: Walter Roberson 2018년 7월 4일
Can any one verify if its correct
function t = delay (e, imax, L1 , t_k)
t = cell(imax,imax);
for i=1:imax
t(i) = L1 * (i-1) + round((i-1) / e)* t_k;
end
Walter Roberson
Walter Roberson 2018년 7월 4일
You mentioned floor() earlier, but here you use round() instead?

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

채택된 답변

Guillaume
Guillaume 2018년 7월 4일
What's stopping you from trying? You'll quickly find out that it errors.
t = zeros(imax);
or probably better
t = zeros(1, imax);
would get rid of the error. Whether or not it's what you want, I don't know.
Note that the same can be achieved without a loop:
im = 0:imax - 1;
t = L1 * im + round(im /e) * t_k;

추가 답변 (0개)

카테고리

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