Bin data into equally spaced intervals

3 次查看(过去 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

采纳的回答

Star Strider
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 —
Bin data into equally spaced intervals - 2020 02 10.png
Experiment with the uniquetol tolerances to get different results.

更多回答(2 个)

Tom Shlomo
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
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
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
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 CenterFile Exchange 中查找有关 Point Cloud Processing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by