Proportional change of values with a fixed sum
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sandra Cortes
le 30 Oct 2017
Commenté : Sandra Cortes
le 31 Oct 2017
Hi!
I´m new using Matlab want to know how to express in the software the change of percentages when I modify one of the values and I have a fixed sum (100%).
Say for example that I have: a= 50%, b=25%, c=25% and the sum is 100%. If I remove a, then values b and c will change into 50% to get the sum of 100%.
0 commentaires
Réponse acceptée
Roger Stafford
le 30 Oct 2017
Modifié(e) : Roger Stafford
le 30 Oct 2017
To put your problem in general terms, suppose you have a row vector of values, v, with some given sum. For the i-th element you wish to change from v(i) = vi1 to v(i) = vi2 and adjust the remaining elements proportionally so as to retain the same overall sum. Then do this:
s = sum(v);
f = (s-vi2)/(s-vi1);
v = [f*v(1:i-1),vi2,f*v(i+1:end)];
Then v should have the same sum as before (except for possible tiny rounding error differences.) (We assume that the sum is different from vi1.)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!