Matlab: fast way to sum ones in binary numbers with Sparse structure?

前端 未结 7 1021
滥情空心
滥情空心 2021-01-07 16:11

Most answers only address the already-answered question about Hamming weights but ignore the point about find and dealing with the sparsity. Apparently the

7条回答
  •  感情败类
    2021-01-07 16:34

    This gives you the rowsums of the binary numbers from the sparse structure.

    >> 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
    
    >> sum(ismember(transpose(dec2bin(find(mlf))),'1'),2)
    
    ans =
    
         1
         0
         0
         2
         1
         1
         2
    

    Hope someone able to find faster rowsummation!

提交回复
热议问题