Proportional change of values with a fixed sum

4 次查看(过去 30 天)
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%.

采纳的回答

Roger Stafford
Roger Stafford 2017-10-30
编辑:Roger Stafford 2017-10-30
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.)
  1 个评论
Sandra Cortes
Sandra Cortes 2017-10-31
Oh I see. Thank you so much for your help!! I applied it by changing several values at the same time and works perfectly!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by