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!