- I create demo data, you can skip this part and replace the variable names (v1, v2) with your data.
- Using discretize, I bin the two vectors according to thier values in steps of 5
- Using accumarray, I sum the vectors within each bin. Another possibility is using groupsummary but one benefit of accumarray is that it will return 0s for missing bins.
- Lastly, plot the result using histogram while specifying the bins and bin counts rather than letting the histogram function do the count calculations.
Histogram (with sum of variables on y-axis)
24 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have two variables consisting of 20 values each. I want to create a histogram where I sum the values in an interval on the x-axis, such as all values between 0 to 5, and plot the associated sum on the y-axis. Hence, I am not interested in the count on the y-axis, but the sum of the variables within that range.
Thanks in advance.
0 件のコメント
採用された回答
Adam Danz
2022 年 12 月 19 日
編集済み: Adam Danz
2022 年 12 月 19 日
Hello Vlatko,
I'm not sure if you want to sum the two vectors and then compute the binned sum or if you want to compute the binned sum on each vector independently. My demo below does the latter, it computes the binned sum on each vector and plots both results. It should be fairly straightforward to sum the data first if that's what you're aiming for.
% Create demo data: two 1x20 vectors of values 0:50
rng default
v1 = 50*rand(1,20);
v2 = 50*rand(1,20);
Assuming your variables are v1 sand v2, the rest should be ready to go with your data.
% Assign each value to a bin
bins = 0:5:50; % Assuming the values are within the range [0,50]
bin1 = discretize(v1,bins);
bin2 = discretize(v2,bins);
% Compute sum of data within each bin
v1sum = accumarray(bin1(:),v1(:));
v2sum = accumarray(bin2(:),v2(:));
% Plot histogram of sums
figure()
histogram('BinEdges',bins,'BinCounts',v1sum,'DisplayName','V1')
hold on
histogram('BinEdges',bins,'BinCounts',v2sum,'DisplayName','v2')
ylabel('bin sum')
xlabel('bin')
legend('location','bestoutside')
3 件のコメント
Adam Danz
2022 年 12 月 19 日
Glad it worked out! In case you're still curious, discretize is used in my example to bin your data. Take this example: I have a vector of random numbers between [1,10], and I want to bin them in two groups: values 1-5 (group 1) and values 6-10 (group 2):
x = randi(10,1,12)
bins = [1 6 10]
discretize(x, bins)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Histograms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!