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

前端 未结 7 925
滥情空心
滥情空心 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:36

    Mex it!


    Save this code as countTransBits.cpp:

    #include "mex.h"
    
    void mexFunction( int nout, mxArray* pout[], int nin, mxArray* pin[] ) {
    mxAssert( nin == 1 && mxIsSparse(pin[0]) && mxGetN( pin[0] ) == 1,
                "expecting single sparse column vector input" );
        mxAssert( nout == 1, "expecting single output" );
    
        // set output, assuming 32 bits, set to 64 if needed
        pout[0] = mxCreateNumericMatrix( 32, 1, mxUINT32_CLASS, mxREAL );
        unsigned int* counter = (unsigned int*)mxGetData( pout[0] );
        for ( int i = 0; i < 32; i++ ) {
            counter[i] = 0;
        }
    
        // start working
        mwIndex *pIr = mxGetIr( pin[0] );
        mwIndex* pJc = mxGetJc( pin[0] );
        double*  pr = mxGetPr( pin[0] );
        for ( mwSize i = pJc[0]; i < pJc[1]; i++ ) {
            if ( pr[i] != 0 ) {// make sure entry is non-zero
                unsigned int entry = pIr[i] + 1; // cast to unsigned int and add 1 for 1-based indexing in Matlab
                int bit = 0;
                while ( entry != 0 && bit < 32 ) {
                    counter[bit] += ( entry & 0x1 ); // count the lsb
                    bit++;
                    entry >>= 1; // shift right
                }
            }
        }
    }
    

    Compile it in Matlab

    >> mex -largeArrayDims -O countTransBits.cpp
    

    Run the code

    >> countTransBits( mlf )
    

    Note that the output count in 32 bins lsb to msb.

    0 讨论(0)
提交回复
热议问题