How can I average one column based on the bins created for the other column?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi All,
I have two columns as a matrix they show the elevaiton (m) of data points (1st column) and thier snow depth (Second column). I want to make bins for elevaiton and see what is the average of the snow depth in that elevation range. I want the elevation data to go to bins of every 12 meter and see i each of this bins how much was the average of the snow depth. So at the end I need two columns one shows the bins (elevation: 0, 12, 24, ...) and the other shows average of snowdepth in that elevation range.
Any idea please
0 Kommentare
Antworten (2)
Adam Danz
am 8 Feb. 2019
Bearbeitet: Adam Danz
am 9 Feb. 2019
This example creates a fake dataset to work with. It then creates bins for the elevation data in steps of 12. Using splitapply, it finds the mean of snow depth for each bin and then puts the data into a table.
% Create fake data [elevation, snowDepth]
data = [randi(100, 1000, 1) + rand(1000,1), randn(1000,1) + 10];
% Create bins
elevationBins = 0:12:max(data(:,1)+11);
% Determine bin membership
[~, bins, binID] = histcounts(data(:,1), elevationBins);
% average snow depth per bin
binMeans = splitapply(@mean, data(:,2), findgroups(binID));
% Put results into table
table(unique(bins(binID))', binMeans, 'VariableNames', {'BinMin', 'BinMean'})
4 Kommentare
Adam Danz
am 9 Feb. 2019
Bearbeitet: Adam Danz
am 11 Feb. 2019
You might not be applying the example correctly to your data. Another possibility is that outliers are messing with the bins (for example, if 1 of your elevations is 100x larger than the rest).
I edited my solution so that it handles outliers better. The change I made was
binMeans = splitapply(@mean, data(:,2), binID); %OLD
binMeans = splitapply(@mean, data(:,2), findgroups(binID)); %NEW
Try to apply your data again to the updated solution. If you continue to have problems, please provide a short sample of your data that I can either copy or download the code.
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!