我找到了一个解决方案,用于使用犰狳here在c ++中使用matlab的accumarray。虽然代码在matlab中应该像它应该的那样,但我的问题是需要花费很多时间。运行大约需要2.2秒,我必须调用此功能360次左右。有没有办法优化这个代码或用armadillo / opencv / boost在c ++中实现accumarray的其他方法?我知道python有一个numpy的bitcount函数,它快速而有效但我无法在c ++中找到任何东西。
谢谢
修改
目前我正在使用以下功能,因为可以在附件链接中看到
代码:
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;
}