How to loop this operation ?

2 views (last 30 days)
Andrea Di Lelio
Andrea Di Lelio on 12 Sep 2019
Answered: Andrei Bobrov on 12 Sep 2019
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 Comments
Stephen23
Stephen23 on 12 Sep 2019
Edited: Stephen23 on 12 Sep 2019
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 on 12 Sep 2019
Edited: Andrea Di Lelio on 12 Sep 2019
Each bad name is a single value. Those are just names now that I used to explain faster.

Sign in to comment.

Accepted Answer

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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!