Azzera filtri
Azzera filtri

Bin data into equally spaced intervals

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Star Strider
Star Strider il 10 Feb 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
Experiment with the uniquetol tolerances to get different results.

Più risposte (2)

Tom Shlomo
Tom Shlomo il 10 Feb 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 Commento
Adam Danz
Adam Danz il 10 Feb 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.

Accedi per commentare.


Adam Danz
Adam Danz il 10 Feb 2020
Modificato: Adam Danz il 11 Feb 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 Commento
Adam Danz
Adam Danz il 10 Feb 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')

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by