Want to weight Histogram entries by value
2 views (last 30 days)
Show older comments
I am using a nx2 array to populate a Histogram
23 2
12 2
85 3
38 3
12 4
09 2
97 4
and want a histogram (or bar chart?) with second attribute as the X axis and the first attribute to be summed up rather then the elements just counted, so the hist would plot
44 2
123 3
111 4
Rather than
3 2
2 3
2 4
Can you help suggest how to use BAR or HISTOGRAM to achieve this please?
Many thanks
0 Comments
Accepted Answer
Voss
on 18 Jun 2022
data = [
23 2
12 2
85 3
38 3
12 4
09 2
97 4
];
[groups,group_ids] = findgroups(data(:,2))
totals = splitapply(@(x)sum(x(:,1)),data,groups)
bar(group_ids,totals)
2 Comments
Voss
on 18 Jun 2022
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!
More Answers (1)
Steven Lord
on 18 Jun 2022
data = [
23 2
12 2
85 3
38 3
12 4
09 2
97 4
];
[V, G] = groupsummary(data(:, 1), data(:, 2), @sum)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!