![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
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Emiliano Ascenzi
le 10 Fév 2020
Modifié(e) : Adam Danz
le 11 Fév 2020
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 commentaires
Réponse acceptée
Star Strider
le 10 Fév 2020
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 commentaires
Plus de réponses (2)
Tom Shlomo
le 10 Fév 2020
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 commentaire
Adam Danz
le 10 Fév 2020
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
le 10 Fév 2020
Modifié(e) : Adam Danz
le 11 Fév 2020
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 commentaire
Adam Danz
le 10 Fév 2020
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')
Voir également
Catégories
En savoir plus sur Point Cloud Processing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!