How to add up values with loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have a problem with replication of my calculations in Excel to script.
I have data set like on the picture belowe.
I created matrix 15x6 with values from yellow columns and I want to calculate values in blue and orange ones.
For aclculating blue ones I used code:
d=15;
ss=[2,2,1,3,1,4,1,1,3,2,2,3,4,1,1]
Tp = readtable("SA_sekw/data_in.xlsx",'Sheet', "Sheet2",'Range', "C2:F8", "ReadVariableNames",true);
Tp = Tp{:,:};
m = length(Tp(:,1));
fp=zeros(d,m+1);
fm=zeros(d,m+1);
Tps=zeros(d,m+1);
for k=1:m; %loop for calculating from input data 15x6 "yelow" matrix
for i=2:d;
Tps(i,k)=Tp(k,ss(1,i));
end;
end;
for dd=1:3 % recalcuating if some value was "0" in previous iteration.
for i=2:d; %loop for calculating "blue" PW value for each row and column
for k=1:m;
val_fp=(Tps(i-1,k+1)+fp(i-1,k+1)-Tps(i,k)); % replicated excel formula
if val_fp <0;
fp(i,k)=0;
else
fp(i,k)=val_fp;
end;
end;
end;
end;
disp(fp)
And I menaged to calulate output like below:
disp(fp)
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 7 411 451 4 0 0
0 122 55 0 0 0 0
298 24 73 529 517 0 0
346 0 451 485 0 0 0
0 102 561 240 0 0 0
405 145 229 55 79 0 0
76 0 0 50 0 0 0
302 437 0 107 121 0 0
398 475 95 150 0 0 0
0 229 176 367 0 0 0
424 479 373 442 332 0 0
448 24 518 572 0 0 0
327 102 561 55 79 0 0
Question is:
WHY values for fp(1:4,1) , fp(1:2,2), fp(1:3,3), fp(1:2,4) are incorrect ?
If i start from i=1:d then script is looking for row number 0 in first iteration.
Any tip will be helpfull.

0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!