Binning data into a new matrix
이전 댓글 표시
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
Guillaume
2020년 1월 6일
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
2020년 1월 6일
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.
채택된 답변
추가 답변 (1개)
Steven Lord
2020년 1월 6일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!