필터 지우기
필터 지우기

How can I average one column based on the bins created for the other column?

조회 수: 11 (최근 30일)
Marmar
Marmar 2019년 2월 8일
편집: Adam Danz 2019년 2월 11일
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

답변 (2개)

Adam Danz
Adam Danz 2019년 2월 8일
편집: Adam Danz 2019년 2월 9일
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
Marmar
Marmar 2019년 2월 9일
my data is not integer. It gives an error that:
Error using splitapply: Group numbers must be a vector of positive integers, and cannot be a sparse vector.
:(
The real data looks like this:
data.jpg
Adam Danz
Adam Danz 2019년 2월 9일
편집: Adam Danz 2019년 2월 11일
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.

댓글을 달려면 로그인하십시오.


Marmar
Marmar 2019년 2월 8일
ANY OTHER IDEASE WILL BE GREATLY APPRECIATED!

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by