Binning data into equally spaced intervals

Hi, I have a large matrix and I'm attempting to bin the data in intervals of 5.
For example, I want all the numbers (most are non integres, ex. 1.2) between 0 to 5 in one bin, then 5 to 10 in another, etc. until I get to 2000 (so 400 bins in total). Also, for each bin, I'd like to find the median or average value, and create a vector with these values.
Any help would be greatly appreciated!

 采纳的回答

One approach —
v = rand(1,1000)*2000;
edges = 0:5:2000;
[counts,edg,bin] = histcounts(v, edges); % Bin Counts
Meanv = accumarray(bin(:),v(:),[],@mean); % Mean Values OF Each Bin
ctrs = edges(1:end-1)+mean(diff(edges)/2); % Bin Centres
figure
subplot(2,1,1)
bar(ctrs, counts)
title('Histogram')
subplot(2,1,2)
scatter(ctrs, Meanv,'.')
title('Mean Values Of Each Bin')
xlabel('Bin')
.

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by