Matlab:汉明权重的索引数组



a包含索引及其出现次数。现在需要用汉明权重更改索引,以便对汉明权重相等的指数进行汇总。如何进行汉明权重索引?在 Matlab 中为此准备好了什么命令吗?

>> a=[1,2;2,3;5,2;10,1;12,2]
     1     2
     2     3
     5     2
    10     1
    12     2
    13     8

>> dec2bin(a(:,1))
ans =
0001
0010
0101
1010
1100
1101

目标:按汉明权重索引事物

HW    Count
1     5 (=2+3)
2     5 (=2+1+2)
3     8 (=8)

您可以按如下方式执行此操作:

a = [1,2;2,3;5,2;10,1;12,2;13,8]

需要添加以下行,以考虑汉明权重为零:

if nnz(a(:,1)) == numel(a(:,1)); a = [0,0;a]; end
% or just
a = [0,0;a];   %// wouldn't change the result

获取索引

rowidx = sum( de2bi(a(:,1)), 2 )

获取总和

sums = accumarray( rowidx+1, a(:,2) ) %// +1 to consider Hammingweight of zero

得到汉明量级向量

HW = unique(rowidx)

返回:

rowidx =
     1
     1
     2
     2
     2
     3

sums =
     5
     5
     8

以及所有一起:

result = [HW, sums]
%or
result = [unique(rowidx), accumarray(rowidx+1,a(:,2))]
result =
     0     0
     1     5
     2     5
     3     8

如果您对0 0行感到困扰,请将其过滤掉

result(sum(result,2)>0,:)

a = [0,2;2,3;5,2;10,1;12,2;13,8]的结果将是:

result =
     0     2
     1     3
     2     5
     3     8

试试这个 -

a = [1     2
     2     3
     5     2
    10     1
    12     2
    13     8]
HW = dec2bin(a(:,1)) - '0'; 
out = accumarray(sum(HW,2), a(:,2), [], @sum);%%// You don't need that "sum" option it seems, as that's the default operation with accumarray
final_out = [unique(sum(HW,2)) out]

输出-

a =
     1     2
     2     3
     5     2
    10     1
    12     2
    13     8

final_out =
     1     5
     2     5
     3     8

相关内容

  • 没有找到相关文章

最新更新