mirror of
https://github.com/processing/processing4.git
synced 2026-02-04 06:09:17 +01:00
add methods to sum up the values in a dict, and return percentages for each
This commit is contained in:
@@ -621,6 +621,25 @@ public class IntDict {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sum all of the values in this dictionary, then return a new FloatDict of
|
||||
* each key, divided by the total sum. The total for all values will be ~1.0.
|
||||
* @return a Dict with the original keys, mapped to their pct of the total
|
||||
*/
|
||||
public FloatDict getPercentages() {
|
||||
double sum = 0;
|
||||
for (int value : valueArray()) {
|
||||
sum += value;
|
||||
}
|
||||
FloatDict outgoing = new FloatDict();
|
||||
for (int i = 0; i < size(); i++) {
|
||||
double percent = value(i) / sum;
|
||||
outgoing.set(key(i), (float) percent);
|
||||
}
|
||||
return outgoing;
|
||||
}
|
||||
|
||||
|
||||
/** Returns a duplicate copy of this object. */
|
||||
public IntDict copy() {
|
||||
IntDict outgoing = new IntDict(count);
|
||||
|
||||
Reference in New Issue
Block a user