Binning data into a new matrix
Show older comments
I have a three column matrix which consists of three columns (x,y,z) as shown below:

I would like to bin the data in column Y but instead of puting the count in a new column i.e bins, I would like to put the the corresponding value in column Z. For example, I would like to bin the value in column Y (-2.5 in blue cell), but instead of putting the count in bins, I would like to put the value in colum Z (12 in red cell) in that bin.
I have written the code below but its putting the count only:
yy = my_matrix(:,2) % taking the second column
% binning
edges = -2.5:0.3:2.5;
N = histcounts(yy,edges);
new_matrix(:,i)= N;
How can I improve it?
3 Comments
Guillaume
on 6 Jan 2020
Typically, you'd have more than one Z per bin (otherwise, it's not really binning!), so what should be put in each bin? The mean? The sum? A vector of values?
Allan Miller
on 6 Jan 2020
Sorry, I don't understand. Here is a simple exampl, given:
% Y Z
M = [-2.5 12
-2.4 15
-2.3 -1
-2.2 5
-2.1 10]
and
edges = [-2.5 -2.2 -1.9];
What exact output do you want?
result = ????
Please write it out as a cell array or matrix.
Accepted Answer
More Answers (1)
Steven Lord
on 6 Jan 2020
0 votes
Use the discretize function. See the "Assign Bin Values" example on that documentation page as I believe it does what you're trying to do.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!