MATLAB Answers

How to binning 2-d data ?

8 views (last 30 days)
ARUNIMA DAS
ARUNIMA DAS on 31 Aug 2021
Answered: Walter Roberson on 1 Sep 2021
I have a data of 29459*13 and I want to bin the data by taking 1500 rows means one bin will be 1500*13. Please can anyone help me out with this regard ?
Thank you for your time.
  4 Comments
ARUNIMA DAS
ARUNIMA DAS on 1 Sep 2021
Extracting static features from each bin and do classification.
Thank you.

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 31 Aug 2021
Edited: Adam Danz on 31 Aug 2021
This approach uses repelem() to generate group IDs by rows.
data is the input matrix.
nRows specifies the number of consecutive rows for each bin (=1500)
binID is the bin ID numbers for each row of data. (1;1;1;....(1500 1s); 2;2;2;...;etc). The last group will have less than 1500 rows since the height of your matrix is not divisible by 1500.
% Create demo data
rng('default')
data = rand(29459,13);
% Specify number of rows per bin
nRows = 1500;
% Create bin IDs
nGroups = ceil(size(data,1)/nRows);
binID = repelem((1:nGroups)', nRows, 1);
binID(size(data,1)+1:end) = [];
Data from bin n is isolated by,
data(binID==n, :)
Compute the average of all data within columns of each bin.
binAvg will be a 20x13 matrix for 20 groups and 13 columns.
binAvg = groupsummary(data, binID(:), 'mean')
binAvg = 20×13
0.5000 0.5159 0.4873 0.5007 0.4993 0.4872 0.4819 0.4938 0.4956 0.4993 0.5045 0.4846 0.5154 0.5077 0.5094 0.5004 0.5163 0.4956 0.5026 0.5085 0.4905 0.4869 0.4879 0.4972 0.4950 0.4860 0.5060 0.4965 0.5001 0.5070 0.4909 0.4989 0.5038 0.5077 0.4988 0.5053 0.4802 0.5021 0.4994 0.4934 0.5038 0.5002 0.4930 0.5084 0.5150 0.5034 0.4975 0.5125 0.4915 0.4923 0.5056 0.5001 0.4976 0.5018 0.5007 0.4973 0.4926 0.5023 0.5014 0.5006 0.5037 0.4944 0.5168 0.4980 0.5061 0.4881 0.5065 0.4885 0.5037 0.4915 0.5004 0.5001 0.4989 0.5018 0.5043 0.5044 0.4988 0.4972 0.5043 0.4882 0.5054 0.4983 0.5083 0.5010 0.5066 0.5024 0.5058 0.4988 0.4827 0.5111 0.5172 0.4997 0.4947 0.4979 0.5099 0.5018 0.5054 0.4957 0.5020 0.5097 0.4956 0.5082 0.4995 0.4991 0.4964 0.4993 0.5043 0.5088 0.5049 0.5020 0.4979 0.4948 0.4879 0.4965 0.4955 0.4974 0.4985 0.4942 0.4950 0.5115 0.4819 0.4928 0.5164 0.5041 0.5058 0.4998 0.4979 0.4961 0.4990 0.4977
Confirm results by selecting a bin and comparing its average with the results above
testBin = 8;
testBinAvg = mean(data(binID==testBin,:));
isequal(testBinAvg, binAvg(testBin,:))
ans = logical
1
Compute the average of all data combined within each bin
binAvgAll will be 20x1 for 20 groups.
binAvgAll = arrayfun(@(g)mean(data(binID==g,:),'all'), unique(binID))
binAvgAll = 20×1
0.4974 0.4988 0.4997 0.5013 0.5010 0.4988 0.5023 0.5015 0.4988 0.4994
Confirm results by selecting a bin and comparing its average with the results above
testBin = 14;
testBinAvg = mean(data(binID==testBin,:),'all');
isequal(testBinAvg, binAvgAll(testBin))
ans = logical
1

Walter Roberson
Walter Roberson on 1 Sep 2021
You will have to decide how you want to handle the last partial bin that is only 959 rows instead of 1500.
You can return a row vector of the extracted features, and the result would be put together into a 20 x whatever array of features. Which you would transpose or not transpose depending on which classification routine you use.

Community Treasure Hunt

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

Start Hunting!