Matlab's Accumarray equivalent in C++

允我心安 提交于 2019-12-11 05:48:47

问题


I found a solution for matlab's accumarray equivalent in c++ with armadillo here. Although the code works like it should in matlab, my problem is that is takes a lot of time. It takes approximately 2.2 seconds to run and i have to call this function around 360 times. Is there a way to optimize this code or anyother way to implement accumarray in c++ with armadillo/opencv/boost? I know python has a bitcount function with numpy which is fast and efficient but i cant find anything in c++.

Thank You

EDIT

Currently I am using the following function, as it can be seen in the link attached

Code:

colvec TestProcessing::accumarray(icolvec cf, colvec T, double nf, int p)
{
    /* ******* Description   *******

    here cf is the matrix of indices

    T is the values whose data is to be
    accumulted in the output array S.

    if T is not given (or is scaler)then accumarray simply converts
    to calculation of histogram of the input data

    nf is the the size of output Array

    nf >= max(cf)
    so pass the argument accordingly

    p is not used in the function 

    ********************************/

    colvec S; // output Array 

    S.set_size(int(nf)); // preallocate the output array 

    for(int i = 0 ; i < (int)nf ; i++)
    {
        // find the indices in cf corresponding to 1 to nf
        // and store in unsigned integer array q1 
        uvec q1 = find(cf == (i+1));
        vec q ;
        double sum1 = 0 ;

        if(!q1.is_empty())
        {
            q = T.elem(q1) ; // find the elements in T having indices in q1 
            // make sure q1 is not empty 

            sum1 = arma::sum(q); // calculate the sum and store in output array 
            S(i) = sum1;
        }

        // if q1 is empty array just put 0 at that particular location
        else
        {
            S(i) = 0 ;
        }
    }
    return S;
}

来源:https://stackoverflow.com/questions/40255975/matlabs-accumarray-equivalent-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!