Bin data into equally spaced intervals
6 ビュー (過去 30 日間)
古いコメントを表示
I have a table with 3 columns (x coordinate, y coordinate, "corrisponding value") and approx. 5k rows (attacched). I'm looking to bin data into equally spaced intervals in x coordinate and y coordinate, then I want to take the average of "corrisponding value" on every bin. I was looking to accumarray function, seems perfect for what i'm looking for, but i can't implement the right code.
I have already readed this, but he has only x coordinate and y coordinate: https://it.mathworks.com/matlabcentral/answers/182552-binning-data-in-equally-spaced-intervals
0 件のコメント
採用された回答
Star Strider
2020 年 2 月 10 日
Try this:
D = load('matlab.mat');
A91 = D.A91;
[Ux,~,ix] = uniquetol(A91(:,1), 5E-7);
[Uy,~,iy] = uniquetol(A91(:,2), 5E-4);
figure
stem3(A91(:,1), A91(:,2), A91(:,3), '.')
grid on
Means = accumarray([ix, iy], A91(:,3), [], @mean);
figure
bar3(Means.')
set(gca, 'XTickLabel',Ux, 'YTickLabel',Uy)
xlabel('X-Coordinate')
ylabel('Y-Coordinate')
zlabel('Mean of ‘Corresponding Value’')
producing this plot —
0 件のコメント
その他の回答 (2 件)
Tom Shlomo
2020 年 2 月 10 日
The following code can be easily extended to any number of dimensions:
x = A91(:,1:2);
val = A91(:,3);
binWidth = [1e-6, 1e-3];
subs = floor( (x-min(x, [],1))./binWidth ) + 1;
means = accumarray(subs, val, max(subs, [], 1), @mean);
1 件のコメント
Adam Danz
2020 年 2 月 10 日
Neat; since there are NaN values in the 3nd col of data, using the omitnan flag may be a good idea in the function applied within accumarray.
Adam Danz
2020 年 2 月 10 日
編集済み: Adam Danz
2020 年 2 月 11 日
This solution uses histcounts2 to bin the x and y values into a 12x12 grid (you can specify the number of bins). Then, accumarray computes the mean within each bin.
load('matlab.mat') % this loads variable "A91" which is a 4864x3 matrix (double)
% Give A91 a better variable name
M = A91;
% Segement row of M into bins.
[binCount,xEdges,yEdges,binX,binY] = histcounts2(M(:,1),M(:,2),[12,12]); % specify number of bins
% Compute mean within each bin
binMeans = accumarray([binX,binY],M(:,3),[],@(x)mean(x,'omitnan'))
binMeans is a 12 x 12 matrix of means within each bin. Bin edges are defined by xEdges and yEdges.
1 件のコメント
Adam Danz
2020 年 2 月 10 日
Note, you can spot-check the binMeans matrix by selecting an x and y bin number and computing the mean with the line of code that follows. The value will match the same coordinate in binMeans.
checkBin = [2,3]; %[x,y]
checkvalue = mean(M(all(checkBin == [binX,binY],2),3),'omitnan')
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!