![Bin data into equally spaced intervals - 2020 02 10.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270856/Bin%20data%20into%20equally%20spaced%20intervals%20-%202020%2002%2010.png)
Bin data into equally spaced intervals
조회 수: 7 (최근 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 —
![Bin data into equally spaced intervals - 2020 02 10.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270856/Bin%20data%20into%20equally%20spaced%20intervals%20-%202020%2002%2010.png)
댓글 수: 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에서 Point Cloud Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!