logarithmic binning of "x" values

5 次查看(过去 30 天)
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,
mean_v = 12×1
NaN NaN NaN NaN NaN 2.0245 0.3650 0.0761 0.0173 0.0031
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
kc = 12×1
1.0e+03 * NaN NaN NaN NaN NaN 0.0825 0.1814 0.3991 0.8780 1.9316
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Is there a function in Matlab which performs the same logarithmic binning?

采纳的回答

Cris LaPierre
Cris LaPierre 2025-5-2
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)
E = 1×13
1.0e+04 * 0.0001 0.0002 0.0005 0.0011 0.0023 0.0052 0.0113 0.0249 0.0549 0.1207 0.2656 0.5843 1.2855
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%Bin data
bIdx = discretize(x,E)
bIdx = 1×63
6 7 7 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[B,BG,BC] = groupsummary(y',bIdx','mean','IncludeEmptyGroups',true)
B = 7×1
2.0245 0.3650 0.0761 0.0173 0.0031 0.0003 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
BG = 7×1
6 7 8 9 10 11 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
BC = 7×1
1 2 4 9 19 26 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean_v = nan(nbins,1);
mean_v(BG) = B
mean_v = 12×1
NaN NaN NaN NaN NaN 2.0245 0.3650 0.0761 0.0173 0.0031
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
kc = nan(nbins,1);
kc(BG) = 0.5*(E(BG(1:end)+1)+E(BG(1:end)))
kc = 12×1
1.0e+03 * NaN NaN NaN NaN NaN 0.0825 0.1814 0.3991 0.8780 1.9316
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 个评论
Cris LaPierre
Cris LaPierre 2025-5-2
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"])
T = 63x2 table
x y ______ _________ 74.5 2.0245 149 0.50611 223.5 0.22388 298 0.12812 372.5 0.080214 447 0.055174 521.5 0.040926 596 0.031831 670.5 0.026172 745 0.021645 819.5 0.017941 894 0.01565 968.5 0.012732 1043 0.011823 1117.5 0.010186 1192 0.0077588
% 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 = 12x3 table
disc_x GroupCount mean_y ________________ __________ __________ [1, 2.2) 0 NaN [2.2, 4.84) 0 NaN [4.84, 10.648) 0 NaN [10.648, 23.426) 0 NaN [23.426, 51.536) 0 NaN [51.536, 113.38) 1 2.0245 [113.38, 249.44) 2 0.36499 [249.44, 548.76) 4 0.076109 [548.76, 1207.3) 9 0.017304 [1207.3, 2656) 19 0.0030951 [2656, 5843.2) 26 0.00031649 [5843.2, 12855] 2 3.7735e-05
Tbinned.kc = movmean(E,[0 1],'Endpoints','discard')';
Tbinned.kc(isnan(Tbinned.mean_y)) = nan
Tbinned = 12x4 table
disc_x GroupCount mean_y kc ________________ __________ __________ ______ [1, 2.2) 0 NaN NaN [2.2, 4.84) 0 NaN NaN [4.84, 10.648) 0 NaN NaN [10.648, 23.426) 0 NaN NaN [23.426, 51.536) 0 NaN NaN [51.536, 113.38) 1 2.0245 82.458 [113.38, 249.44) 2 0.36499 181.41 [249.44, 548.76) 4 0.076109 399.1 [548.76, 1207.3) 9 0.017304 878.01 [1207.3, 2656) 19 0.0030951 1931.6 [2656, 5843.2) 26 0.00031649 4249.6 [5843.2, 12855] 2 3.7735e-05 9349.1

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2025-5-2
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)
sampleData = 5×5
1 47 53 35 95 7 27 92 56 7 24 55 97 43 60 12 92 66 51 96 80 38 92 78 73
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
nonlinearBinEdges = (1:10).^2
nonlinearBinEdges = 1×10
1 4 9 16 25 36 49 64 81 100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
groupNumber = discretize(sampleData, nonlinearBinEdges)
groupNumber = 5×5
1 6 7 5 9 2 5 9 7 2 4 7 9 6 7 3 9 8 7 9 8 6 9 8 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 个评论
Sim
Sim 2025-5-2
编辑:Sim 2025-5-2
thanks a lot both @Steven Lord and @Cris LaPierre!!
I accept the @Cris LaPierre’s answer because it’s a bit clearer to me, but that doesn’t take away from the fact that @Steven Lord’s response is also excellent!!!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by