MatLab-histc多边向量
考虑这一点:MatLab-histc多边向量,matlab,vectorization,binning,Matlab,Vectorization,Binning,考虑这一点: a = [1 ; 7 ; 13]; edges = [1, 3, 6, 9, 12, 15]; [~, bins] = histc(a, edges) bins = 1 3 5 现在,我希望有相同的输出,但每个值有不同的“边”向量,即矩阵而不是边向量。例如: a = [1 ; 7 ; 13]; edges = [ 1, 3, 6 ; 1, 4, 15 ; 1, 20, 30]; edges = 1 3
a = [1 ; 7 ; 13];
edges = [1, 3, 6, 9, 12, 15];
[~, bins] = histc(a, edges)
bins =
1
3
5
现在,我希望有相同的输出,但每个值有不同的“边”向量,即矩阵而不是边向量。例如:
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]
我可以在for
循环中使用histc
来实现这一点,因为我试图避免循环。如果您将数组转换为单元格数组,您可以尝试
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);
你也可以这样做:
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
有没有一种简单的方法可以从矩阵传递到射线?与H.Muster的解决方案相比,有没有性能上的提高?我喜欢它,因为它不必在单元数组中变换矩阵。@Johnny5:您可以测试这两种方法。创建更大的数据并使用tic/toc比较计时。。。我怀疑这个更快,但不要相信我的话:)