# How to calculate repeated calculation using LOOP?

4 vues (au cours des 30 derniers jours)
Undrakh le 12 Nov 2022
Modifié(e) : Karim le 13 Nov 2022
I have 2 tables as below:
NetCh - has 12 column (var1 var2 ... var12) and 9 rows
yPeriod1 - has 1 column and 9 rows
I would like to calculation within each column in NetCh for 12 times and write 12 results (Der1 Der2 .. Der12) in a new table.
Now I have to do by mannually all of them.
Can you please tell me make it small code using LOOP.
NetCh1 = table(NetCh.Var1)
NetCh1.Hangal = (NetCh1{:,1}).*(yPeriod1{:,1})
NetCh1.Haliun = (yPeriod1{:,1}).^2
Tr1 = sum(yPeriod1{:,1})
Br1 = sum(NetCh1{:,1},'omitnan')
Hr1 = sum(NetCh1.Hangal,'omitnan')
Qr1 = sum(NetCh1.Haliun)
Der1 = Br1*Qr1-Tr1*Hr1
NetCh2 = table(NetCh.Var2)
NetCh2.Hangal = (NetCh2{:,1}).*(yPeriod1{:,1})
NetCh2.Haliun = (yPeriod1{:,1}).^2
Tr2 = sum(yPeriod1{:,1})
Br2 = sum(NetCh2{:,1},'omitnan')
Hr2 = sum(NetCh2.Hangal,'omitnan')
Qr2 = sum(NetCh2.Haliun)
Der2 = Br2*Qr2-Tr2*Hr2
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Karim le 12 Nov 2022
Modifié(e) : Karim le 13 Nov 2022
Note that you can add data (e.g. a .mat file) to your question using the paperclip symbol. Without it is is difficult to validate the process.
To give you an idea on how a for loop could look like:
% EDIT, updated the routine after the OP provided example data
NetCh = 9×13 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 ___________ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ {''x2014''} NaN NaN NaN NaN NaN NaN NaN NaN 394.01 396.81 398.17 401.94 {''x2015''} 395.06 401.02 403.83 403.84 399.69 398.05 396.48 394.69 398.28 400.21 398.83 403.9 {''x2016''} 405.03 407.51 407.61 408.24 406.89 397.07 398.52 402.05 395.93 397.88 398.82 406 {''x2017''} 405.3 407.8 408.44 410.49 409.54 404.08 404.25 403.03 403.75 404.21 404.41 407.65 {''x2018''} 407.82 409.4 410.47 416.56 410.47 410.48 401.97 402.19 400.87 408.06 408.8 409.09 {''x2019''} 411.23 411.56 414.78 416.27 413.51 406.49 406.72 407.75 407.06 409.13 409.92 414.33 {''x2020''} 414.91 417.22 416.72 415.13 417.22 414.55 404.2 399.96 410.46 414.34 413.88 417.01 {''x2021''} 414.92 409.11 417.77 420.33 417.7 415.39 412.6 407.96 410.13 415.54 415.26 417.36 {''x2022''} 426.89 421.19 422.16 424.54 NaN NaN NaN NaN NaN NaN NaN NaN
% skip 1 column as it seems to be the vaiable name
numVar = size(NetCh,2)-1;
yPeriodFor = (1:9)';
% create an array to save the results
Der = zeros(numVar,1);
for i = 1:numVar
% extract the current column
% +1 to skip the first column
currData = NetCh{:,i+1};
% do the processing
Hangal = currData.*yPeriodFor;
Haliun = yPeriodFor.^2;
% evaluate the sum's
Tr = sum(yPeriodFor);
Br = sum(currData,'omitnan');
Hr = sum(Hangal,'omitnan');
Qr = sum(Haliun);
% evaluate and save the result
Der(i) = Br*Qr-Tr*Hr;
end
% display the results
Der
Der = 12×1
1.0e+05 * 1.1619 1.1914 1.1913 1.1940 1.6896 1.6674 1.6668 1.6724 2.6110 2.6242
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Karim le 12 Nov 2022
I made an update to the answer, the reason for the error is that the first column contains text... you need to skip this column in the for loop. See the adjusted answer.
Undrakh le 13 Nov 2022
Modifié(e) : Undrakh le 13 Nov 2022
Hi Karim,
Thank you a lot. This worked for me. :)

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by