Separate a matrix into bins based on first column values

I want to seperate a matrix that has two columns of values into bins based on the value of the first column. I would like to be seperate in 1 step incraments from -15 to 15 for the first column and grab the second column value with it so I can analyze the second column values during different sections of column one. Is there a way to do this. I attached the data file that I am trying to seperate into bins. Thank you for your help!

 Accepted Answer

load('DataRoll.mat')
plot(DataRoll(:,1),DataRoll(:,2)) % plot all data for reference
grid on
set(gca(), ...
'Box','on', ...
'Layer','top', ...
'XMinorGrid','on', ...
'XLim',[-15 16]);
bin_idx = discretize(DataRoll(:,1),-15:16); % using -15:15 misses the data above 15
binned_data = splitapply(@(x){x},DataRoll,findgroups(bin_idx))
binned_data = 31×1 cell array
{108×2 double} {118×2 double} {190×2 double} {252×2 double} {260×2 double} {228×2 double} {378×2 double} {429×2 double} {385×2 double} {250×2 double} {301×2 double} {334×2 double} {455×2 double} {546×2 double} {496×2 double} {552×2 double}
figure
hold on
cellfun(@(x)plot(x(:,1),x(:,2),'.'),binned_data);
set(gca(), ...
'Box','on', ...
'Layer','top', ...
'XGrid','on', ...
'XMinorGrid','on', ...
'MinorGridLineStyle','-', ...
'MinorGridColor','k', ...
'MinorGridAlpha',1, ...
'GridAlpha',1, ...
'XLim',[-15 16]);

6 Comments

Would you know how to find the average of the 2nd column in each bin?
% loading and binning the data as before:
load('DataRoll.mat')
bin_idx = discretize(DataRoll(:,1),-15:16); % using -15:15 misses the data above 15
binned_data = splitapply(@(x){x},DataRoll,findgroups(bin_idx));
% calculating the mean of the second column of data in each bin:
binned_mean = cellfun(@(x)mean(x(:,2),1),binned_data)
binned_mean = 31×1
-3.7843 -4.2714 -5.1898 -8.5879 -8.7605 -8.9249 -8.8929 -7.9495 -8.0889 -9.0207

Sign in to comment.

More Answers (1)

dpb
dpb on 17 Jun 2022
Edited: dpb on 17 Jun 2022
load DataRoll
bar(N)
xlim([0.25 31.75])
[min(DataRoll(:,1)) max(DataRoll(:,1))]
[N,e,g]=histcounts(DataRoll(:,1),-15:16);
t=[array2table(DataRoll),table(g)];
groupsummary(t,"g","std","DataRoll2")
You can do whatever you want with the grouping variable calculation -- also see splitapply rowfun and/or varfun as well as groupfilter
NB: the range of your data are from
[min(DataRoll(:,1)) max(DataRoll(:,1))]
ans =
-14.9047 15.5137
>>
so the edges vector needs must cover 16 as upper end to bin all elements in the first column.

Categories

Asked:

on 17 Jun 2022

Commented:

on 17 Jun 2022

Community Treasure Hunt

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

Start Hunting!