How to loop this operation ?

1 次查看(过去 30 天)
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 中查找有关 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