How to loop this operation ?

조회 수: 1 (최근 30일)
Andrea Di Lelio
Andrea Di Lelio 2019년 9월 12일
답변: Andrei Bobrov 2019년 9월 12일
Hello everybody,
I am trying to think about a loop in order to produce with 4/5 rows of code a simple result.
I have one vector of dimension 300x1 (called TarG) and the next ones are the operation that I would like to loop.
D_11 = TarG(300) / 100;
D_12 = (TarG(280) / TarG(281)) * D_11;
D_13 = (TarG(260) / TarG(261)) * D_12;
D_14 = (TarG(240) / TarG(241)) * D_13;
D_15 = (TarG(220) / TarG(221)) * D_14;
D_16 = (TarG(200) / TarG(201)) * D_15;
D_17 = (TarG(180) / TarG(181)) * D_16;
D_18 = (TarG(160) / TarG(161)) * D_17;
D_19 = (TarG(140) / TarG(141)) * D_18;
D_20 = (TarG(120) / TarG(121)) * D_19;
D_21 = (TarG(100) / TarG(101)) * D_20;
D_22 = (TarG(80) / TarG(81)) * D_21;
D_23 = (TarG(60) / TarG(61)) * D_22;
D_24 = (TarG(40) / TarG(41)) * D_23;
D_25 = (TarG(20) / TarG(21)) * D_24;
Tot_TarG = [D_11; D_12; D_13; D_14; D_15; D_16; D_17; D_18; D_19; D_20; D_21; D_22; D_23; D_24; D_25];
Is there someone that could help me?
Thanks in advance
  댓글 수: 2
Stephen23
Stephen23 2019년 9월 12일
편집: Stephen23 2019년 9월 12일
What sizes are the very unfortunately named D_11, D_12, etc.?
Using numbered variables is s sign that you are doing something wrong. You should read this:
Andrea Di Lelio
Andrea Di Lelio 2019년 9월 12일
편집: Andrea Di Lelio 2019년 9월 12일
Each bad name is a single value. Those are just names now that I used to explain faster.

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 9월 12일
ii = 300:-20:20;
D = TarG(ii)./[100;TarG(ii(2:end) + 1)];
Tot_TarG = cumprod(D);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by