2

考虑一下:

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循环内做到这一点,我试图避免循环。

4

2 回答 2

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);
于 2012-07-03T13:36:21.690 回答
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
于 2012-07-03T15:16:37.820 回答