是否有C/C ++中的accumarray()的示例 [英] Is there an example for accumarray() in C/C++

查看:76
本文介绍了是否有C/C ++中的accumarray()的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正试图了解MATLAB的accumarray函数,希望为我们的理解编写相同的C/C ++代码.有人可以帮助我们提供示例代码/伪代码吗?

We are trying to understand accumarray function of MATLAB, wanted to write C/C++ code for the same for our understanding. Can someone help us with a sample/pseudo code?

推荐答案

根据文档

该函数按如下方式处理输入:

The function processes the input as follows:

  1. 找出子项中有多少个唯一索引.每个唯一索引在输出数组中定义一个bin.中的最大索引值 subs确定输出数组的大小.

  1. Find out how many unique indices there are in subs. Each unique index defines a bin in the output array. The maximum index value in subs determines the size of the output array.

找出每个索引重复多少次.

Find out how many times each index is repeated.

这确定了将在输出数组中每个bin上累积多少val元素.

This determines how many elements of vals are going to be accumulated at each bin in the output array.

创建一个输出数组.输出数组的大小为max(subs)或大小为sz.

Create an output array. The output array is of size max(subs) or of size sz.

使用subs中的索引值将val中的条目累积到bin中,并对每个bin中的条目应用乐趣.

Accumulate the entries in vals into bins using the values of the indices in subs and apply fun to the entries in each bin.

为子程序未引用的位置填充输出中的值.默认填充值为零;使用fillval设置其他 值.

Fill the values in the output for positions not referred to by subs. Default fill value is zero; use fillval to set a different value.

因此,翻译成C ++(这是未经测试的代码),

So, translating to C++ (this is untested code),

template< typename sub_it, typename val_it, typename out_it,
          typename fun = std::plus< typename std::iterator_traits< val_it >::value_type >,
          typename T = typename fun::result_type >
out_it accumarray( sub_it first_index, sub_it last_index,
                   val_it first_value, // val_it last_value, -- 1 value per index
                   out_it first_out,
                   fun f = fun(), T fillval = T() ) {
    std::size_t sz = std::max_element( first_index, last_index ); // 1. Get size.
    std::vector< bool > used_indexes; // 2-3. remember which indexes are used

    std::fill_n( first_out, sz, T() ); // 4. initialize output

    while ( first_index != last_index ) {
        std::size_t index = * first_index;
        used_indexes[ index ] = true; // 2-3. remember that this index was used
        first_out[ index ] = f( first_out[ index ], * first_value ); // 5. accumulate
        ++ first_value;
        ++ first_index;
    }

    // If fill is different from zero, reinitialize untouched values
    if ( fillval != T() ) {
        out_it fill_it = first_out;
        for ( std::vector< bool >::iterator used_it = used_indexes.begin();
              used_it != used_indexes.end(); ++ used_it ) {
            if ( * used_it ) * fill_it = fillval;
        }
    }

    return first_out + sz;
}

这有一些缺点,例如,累加函数被调用,而不是整个列向量都被调用一次.输出放置在first_out引用的预分配存储中.索引向量的大小必须与值向量的大小相同.但是大多数功能应该都可以很好地捕获.

This has a few shortcomings, for example the accumulation function is called repeatedly instead of once with the entire column vector. The output is placed in pre-allocated storage referenced by first_out. The index vector must be the same size as the value vector. But most of the features should be captured pretty well.

这篇关于是否有C/C ++中的accumarray()的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆