【问题标题】:Given values and bin edges, find the bin of each value in MATLAB给定值和 bin 边缘,在 MATLAB 中找到每个值的 bin
【发布时间】:2021-02-21 20:14:44
【问题描述】:

我有一组值,我想找出它们各自属于哪个 bin 更紧凑,可能是矢量化的,最重要的是比下面的更快,

values = rand(1,3)*50;
bins = 0:10:50;
binValues = nan(size(values));
for valueIndex = 1:length(values)
    dif = bins - values(valueIndex);
    [~,locat] = min(abs(dif));
    %to see on which side it is
    if dif(locat)>0
        locat = locat - 1;
    end
    %if its outside the bins:
    if locat==0 || locat==length(bins)
        locat=nan;
    end
    binValues(valueIndex) = locat;
end

values
bins 
binValues

例如,

values =

   28.6037   37.7998   30.8294


bins =

     0    10    20    30    40    50


binValues =

     3     4     4

【问题讨论】:

    标签: matlab find bin


    【解决方案1】:

    也许,看看R2015a中介绍的MATLAB的discretize函数。使用此功能,您可以将循环替换为:

    binValues = discretize(values, bin, 'IncludeEdge', 'right');
    

    您可以使用 IncludeEdge 参数将 bin 边缘的值分配给左侧或右侧 bin。例如,10 是否属于 bin 1(即:..., "IncludeEdge", "left")或 bin 2(即:..., "IncludeEdge", "right")。

    最后,请注意,bin 范围之外的值将被分配 bin 值 NaN。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多