Matlab的:快速的方式来总结在二进制数那些稀疏结构? [英] Matlab: fast way to sum ones in binary numbers with Sparse structure?
问题描述
的大多数的答案只能解决关于<一个本已回答的问题href=\"http://stackoverflow.com/questions/1024904/calculating-hamming-weight-efficiently-in-matlab\">Hamming 中的权重,但忽略有关点找到
和处理稀疏。显然,答案由这里晒 解决有关找到一点 - 但我还不能验证。我的回答这里不利用其他的答案,如bitshifting,但不够好例子回答的聪明才智。的
Most answers only address the already-answered question about Hamming weights but ignore the point about find
and dealing with the sparsity. Apparently the answer by Shai here addresses the point about find -- but I am not yet able to verify it. My answer here does not utilise the ingenuity of other answers such as the bitshifting but good enough example answer.
输入
>> mlf=sparse([],[],[],2^31+1,1);mlf(1)=10;mlf(10)=111;mlf(77)=1010;
>> transpose(dec2bin(find(mlf)))
ans =
001
000
000
011
001
010
101
目标
1
0
0
2
1
1
2
快速计算那些在二进制数与稀疏结构的金额?
推荐答案
您可以用吨的方式做到这一点。我认为,最简单的是
You can do this in tons of ways. The simplest I think would be
% Example data
F = [268469248 285213696 536904704 553649152];
% Solution 1
sum(dec2bin(F)-'0',2)
和最快的(如发现<一个href=\"http://stackoverflow.com/questions/1024904/calculating-hamming-weight-efficiently-in-matlab\">here):
% Solution 2
w = uint32(F');
p1 = uint32(1431655765);
p2 = uint32(858993459);
p3 = uint32(252645135);
p4 = uint32(16711935);
p5 = uint32(65535);
w = bitand(bitshift(w, -1), p1) + bitand(w, p1);
w = bitand(bitshift(w, -2), p2) + bitand(w, p2);
w = bitand(bitshift(w, -4), p3) + bitand(w, p3);
w = bitand(bitshift(w, -8), p4) + bitand(w, p4);
w = bitand(bitshift(w,-16), p5) + bitand(w, p5);
这篇关于Matlab的:快速的方式来总结在二进制数那些稀疏结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!