Want to weight Histogram entries by value

4 ビュー (過去 30 日間)
simon lumsdon
simon lumsdon 2022 年 6 月 18 日
編集済み: Vlatko Milic 2022 年 12 月 19 日
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

採用された回答

Voss
Voss 2022 年 6 月 18 日
data = [
23 2
12 2
85 3
38 3
12 4
09 2
97 4
];
[groups,group_ids] = findgroups(data(:,2))
groups = 7×1
1 1 2 2 3 1 3
group_ids = 3×1
2 3 4
totals = splitapply(@(x)sum(x(:,1)),data,groups)
totals = 3×1
44 123 109
bar(group_ids,totals)
  3 件のコメント
Voss
Voss 2022 年 6 月 18 日
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!
Vlatko Milic
Vlatko Milic 2022 年 12 月 19 日
編集済み: Vlatko Milic 2022 年 12 月 19 日
Do you have any idea of how I could make the corresponding procedure but with set intervals on the x-axis, i.e. 2.5-3.5 etc.? Your solution is quite close to what I want to accomplish, but without the intervals...

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 6 月 18 日
data = [
23 2
12 2
85 3
38 3
12 4
09 2
97 4
];
[V, G] = groupsummary(data(:, 1), data(:, 2), @sum)
V = 3×1
44 123 109
G = 3×1
2 3 4

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by