1

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)
4

2 回答 2

2

你可以这样做:

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

需要添加以下行,以考虑也为零的hammingweight:

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

得到 Hammingweight 向量

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
于 2014-04-05T12:11:18.513 回答
1

尝试这个 -

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
于 2014-04-05T12:10:30.770 回答