logarithmic binning of "x" values
5 views (last 30 days)
Show older comments
The following code performs a logarithmic binning of the "x" values:
clear all;clc;
% Input
x = [74.5, 149, 223.5, 298, 372.5, 447, 521.5, 596, 670.5, 745, 819.5, 894, 968.5, 1043, 1117.5, 1192, 1266.5, 1341, 1415.5, 1490, 1564.5, 1639, 1713.5, 1788, 1862.5, 1937, 2011.5, 2086, 2160.5, 2235, 2309.5, 2384, 2458.5, 2533, 2607.5, 2682, 2756.5, 2831, 2905.5, 2980, 3054.5, 3129, 3203.5, 3278, 3352.5, 3427, 3501.5, 3576, 3650.5, 3725, 3799.5, 3948.5, 4023, 4097.5, 4172, 4470, 4544.5, 4619, 4693.5, 4768, 5364, 6034.5, 6556];
y = [2.0245, 0.50611, 0.22388, 0.12812, 0.080214, 0.055174, 0.040926, 0.031831, 0.026172, 0.021645, 0.017941, 0.01565, 0.012732, 0.011823, 0.010186, 0.0077588, 0.0074896, 0.006543, 0.0060311, 0.0042972, 0.0045473, 0.0037618, 0.0034599, 0.0031831, 0.0026738, 0.0022037, 0.0024757, 0.0020463, 0.0017562, 0.002016, 0.0016429, 0.0013926, 0.0012539, 0.0011234, 0.00090946, 0.00097261, 0.00060221, 0.00075389, 0.0008978, 0.00063662, 0.00054346, 0.0006063, 0.00051818, 0.00036172, 0.00021221, 0.00034599, 0.00020318, 0.00019894, 6.4961e-05, 0.00019099, 0.00018724, 0.00018018, 5.8946e-05, 0.00011575, 0.00017052, 0.00015915, 5.2182e-05, 5.134e-05, 5.0525e-05, 4.9736e-05, 4.421e-05, 3.9298e-05, 3.6172e-05];
% logarithmic binning
factor = 2.2;
binsF = 1;
i = 2;
while( binsF(i-1) < max( x ) )
binsF(i) = binsF(i-1)*factor;
i = i + 1;
end
mean_v = [];
kc = zeros( length(binsF) - 1,1 );
for( i = 1:length( binsF ) - 1 )
in = find( x >= binsF(i) & x < binsF(i+1) );
if ~isempty(in)
mean_v = [mean_v;mean(y(in))];
kc(i) = 0.5*(binsF(i + 1)+binsF(i));
else
mean_v = [mean_v; NaN]; % Handle empty bins
kc(i) = NaN;
end
end
mean_v, kc,
Is there a function in Matlab which performs the same logarithmic binning?
0 Comments
Accepted Answer
Cris LaPierre
on 2 May 2025
I'm not sure the end result is much simpler, but the function that comes to mind is discretize. You'd still need to define your bin edges.
% Input
x = [74.5, 149, 223.5, 298, 372.5, 447, 521.5, 596, 670.5, 745, 819.5, 894, 968.5, 1043, 1117.5, 1192, 1266.5, 1341, 1415.5, 1490, 1564.5, 1639, 1713.5, 1788, 1862.5, 1937, 2011.5, 2086, 2160.5, 2235, 2309.5, 2384, 2458.5, 2533, 2607.5, 2682, 2756.5, 2831, 2905.5, 2980, 3054.5, 3129, 3203.5, 3278, 3352.5, 3427, 3501.5, 3576, 3650.5, 3725, 3799.5, 3948.5, 4023, 4097.5, 4172, 4470, 4544.5, 4619, 4693.5, 4768, 5364, 6034.5, 6556];
y = [2.0245, 0.50611, 0.22388, 0.12812, 0.080214, 0.055174, 0.040926, 0.031831, 0.026172, 0.021645, 0.017941, 0.01565, 0.012732, 0.011823, 0.010186, 0.0077588, 0.0074896, 0.006543, 0.0060311, 0.0042972, 0.0045473, 0.0037618, 0.0034599, 0.0031831, 0.0026738, 0.0022037, 0.0024757, 0.0020463, 0.0017562, 0.002016, 0.0016429, 0.0013926, 0.0012539, 0.0011234, 0.00090946, 0.00097261, 0.00060221, 0.00075389, 0.0008978, 0.00063662, 0.00054346, 0.0006063, 0.00051818, 0.00036172, 0.00021221, 0.00034599, 0.00020318, 0.00019894, 6.4961e-05, 0.00019099, 0.00018724, 0.00018018, 5.8946e-05, 0.00011575, 0.00017052, 0.00015915, 5.2182e-05, 5.134e-05, 5.0525e-05, 4.9736e-05, 4.421e-05, 3.9298e-05, 3.6172e-05];
% logarithmic binning
factor = 2.2;
nbins = ceil(log2(max(x))/log2(2.2));
E = factor.^(0:nbins)
%Bin data
bIdx = discretize(x,E)
[B,BG,BC] = groupsummary(y',bIdx','mean','IncludeEmptyGroups',true)
mean_v = nan(nbins,1);
mean_v(BG) = B
kc = nan(nbins,1);
kc(BG) = 0.5*(E(BG(1:end)+1)+E(BG(1:end)))
1 Comment
Cris LaPierre
on 2 May 2025
If the data is in a table, then groupsummary might do what you want as well. Again, you still need to define your bin edges.
% Input
x = [74.5, 149, 223.5, 298, 372.5, 447, 521.5, 596, 670.5, 745, 819.5, 894, 968.5, 1043, 1117.5, 1192, 1266.5, 1341, 1415.5, 1490, 1564.5, 1639, 1713.5, 1788, 1862.5, 1937, 2011.5, 2086, 2160.5, 2235, 2309.5, 2384, 2458.5, 2533, 2607.5, 2682, 2756.5, 2831, 2905.5, 2980, 3054.5, 3129, 3203.5, 3278, 3352.5, 3427, 3501.5, 3576, 3650.5, 3725, 3799.5, 3948.5, 4023, 4097.5, 4172, 4470, 4544.5, 4619, 4693.5, 4768, 5364, 6034.5, 6556];
y = [2.0245, 0.50611, 0.22388, 0.12812, 0.080214, 0.055174, 0.040926, 0.031831, 0.026172, 0.021645, 0.017941, 0.01565, 0.012732, 0.011823, 0.010186, 0.0077588, 0.0074896, 0.006543, 0.0060311, 0.0042972, 0.0045473, 0.0037618, 0.0034599, 0.0031831, 0.0026738, 0.0022037, 0.0024757, 0.0020463, 0.0017562, 0.002016, 0.0016429, 0.0013926, 0.0012539, 0.0011234, 0.00090946, 0.00097261, 0.00060221, 0.00075389, 0.0008978, 0.00063662, 0.00054346, 0.0006063, 0.00051818, 0.00036172, 0.00021221, 0.00034599, 0.00020318, 0.00019894, 6.4961e-05, 0.00019099, 0.00018724, 0.00018018, 5.8946e-05, 0.00011575, 0.00017052, 0.00015915, 5.2182e-05, 5.134e-05, 5.0525e-05, 4.9736e-05, 4.421e-05, 3.9298e-05, 3.6172e-05];
% add to table
T = table(x',y','VariableNames',["x","y"])
% logarithmic binning
factor = 2.2;
nbins = ceil(log2(max(x))/log2(2.2));
E = factor.^(0:nbins);
% Bin data and compute mean
Tbinned = groupsummary(T,"x",E,"mean","y",'IncludeMissingGroups',true,"IncludeEmptyGroups",true)
Tbinned.kc = movmean(E,[0 1],'Endpoints','discard')';
Tbinned.kc(isnan(Tbinned.mean_y)) = nan
More Answers (1)
Steven Lord
on 2 May 2025
Use histogram, histcounts, discretize, and/or groupsummary. Since your application is that you want to perform an operation summarizing the data in each bin, using discretize or findgroups to generate a vector of group numbers then using those group vectors with groupsummary to compute the mean of the data in each bin is probably the right approach.
sampleData = randi(100, 5, 5)
nonlinearBinEdges = (1:10).^2
groupNumber = discretize(sampleData, nonlinearBinEdges)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!