MatLab - 有许多边缘矢量的histc

时间:2012-07-03 13:10:04

标签: matlab vectorization binning

考虑一下:

a = [1 ; 7 ; 13];
edges = [1, 3, 6, 9, 12, 15];

[~, bins] = histc(a, edges)

bins =

     1
     3
     5

现在我希望具有相同的输出,但每个a值具有不同的“边缘”矢量,即矩阵而不是边缘矢量。例如:

   a = [1 ; 7 ; 13];
    edges = [ 1, 3, 6 ; 1, 4, 15 ; 1, 20, 30];

edges =

     1     3     6
     1     4    15
     1    20    30


    indexes = theFunctionINeed(a, edges);

    indexes = 
            1   % 1 inside [1, 3, 6]
            2   % 7 indide [1, 4, 15]
            1   %13 inside [1, 20, 30]

我可以在histc循环中使用for执行此操作,我正在尝试避免循环。

2 个答案:

答案 0 :(得分:3)

如果将数组转换为单元格数组,则可以尝试

a = {1 ; 7 ; 13};
edges = {[ 1, 3, 6 ];[ 1, 4, 15] ; [1, 20, 30]};

[~, indexes] = cellfun(@histc, a, edges,'uniformoutput', false)

这导致

indexes = 

    [1]
    [2]
    [1]

〜编辑

要将矩阵转换为单元格数组,可以使用num2cell

a  = num2cell(a); 
edges = num2cell(edges, 2);

答案 1 :(得分:2)

你也可以这样做:

a = [1; 7; 13];
edges = [1 3 6; 1 4 15; 1 20 30];

bins = sum(bsxfun(@ge, a, edges), 2)

结果:

>> bins
bins =
     1
     2
     1