数组元素的重复副本:MATLAB 中的游程解码
我正在尝试使用“值”数组和“计数器”数组将多个值插入到数组中。例如,如果:
a=[1,3,2,5]
b=[2,2,1,3]
我希望某个函数的输出
c=somefunction(a,b)
为
c=[1,1,3,3,2,5,5,5]
其中 a(1) 重复 b(1) 次,a(2) 重复 b(2) 次,等等...
是否有内置函数在 MATLAB 中是这样做的吗?如果可能的话,我想避免使用 for 循环。我尝试过“repmat()”和“kron()”的变体,但没有成功。
这基本上是运行长度编码
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题陈述
我们有一个值数组,
vals
和游程长度,runlens
:我们需要重复
vals
中的每个元素乘以运行透镜。因此,最终输出将是:预期方法
MATLAB 最快的工具之一是
cumsum
在处理不规则图案的矢量化问题时非常有用。在所述问题中,不规则性是由runlens
中的不同元素带来的。现在,要利用 cumsum,我们需要在这里做两件事:初始化一个由 zeros 组成的数组,并将“适当的”值放置在 zeros 数组上的“关键”位置,例如应用“
cumsum
”后,我们最终会得到一个重复vals
次runlens
次的最终数组。步骤:让我们对上述步骤进行编号,以便为预期方法提供更简单的视角:
1)初始化零数组:长度必须是多少?由于我们重复
runlens
次,zeros 数组的长度必须是所有runlens
的总和。2) 查找关键位置/索引:现在这些关键位置位于 Zeros 数组中,其中
vals
中的每个元素开始重复。因此,对于
runlens = [2,2,1,3]
,映射到 Zeros 数组的关键位置将是:3) 找到合适的值:使用
之前要敲定的最后一个钉子cumsum
是将“适当的”值放入这些关键位置。现在,由于我们很快就会进行cumsum
,如果您仔细思考,您将需要values
的差异化
版本,其中diff
,以便cumsum
这些将带回我们的价值观
。由于这些差异值将放置在由runlens
距离分隔的零数组上,因此在使用cumsum
后,我们将重复每个vals
元素runlens
次作为最终输出。解决方案代码
这是缝合上述所有步骤的实现 -
预分配黑客
可以看出,上面列出的代码使用了零预分配。现在,根据这篇关于更快预分配的未公开的 MATLAB 博客,人们可以实现更快的预分配-allocation with -
包装:函数代码
为了包装所有内容,我们将有一个紧凑的函数代码来实现这种游程解码,如下所示 -
基准测试
基准测试代码
接下来列出是基准测试代码,用于比较本文中所述的 cumsum+diff 方法与其他<
MATLAB 2014B
上基于 code>cumsum-only 的方法 -rld_cumsum.m
的关联函数代码:运行时和加速图强>
结论
与
cumsum-only
方法相比,所提出的方法似乎给我们带来了显着的加速,该方法大约是 3倍!为什么这种新的基于
cumsum+diff
的方法比以前的cumsum-only
方法更好?嗯,原因的本质在于最后的结果
cumsum-only
方法的步骤,需要将“cumsumed”值映射到vals
。在基于 cumsum+diff 的新方法中,我们执行的是diff(vals)
,而 MATLAB 仅处理n
个元素(其中 n 是runLengths 的数量)与仅 cumsum
方法的sum(runLengths)
元素映射数量相比,该数量必须比多很多倍n
因此,这种新方法带来了显着的加速!Problem Statement
We have an array of values,
vals
and runlengths,runlens
:We are needed to repeat each element in
vals
times each corresponding element inrunlens
. Thus, the final output would be:Prospective Approach
One of the fastest tools with MATLAB is
cumsum
and is very useful when dealing with vectorizing problems that work on irregular patterns. In the stated problem, the irregularity comes with the different elements inrunlens
.Now, to exploit
cumsum
, we need to do two things here: Initialize an array ofzeros
and place "appropriate" values at "key" positions over the zeros array, such that after "cumsum
" is applied, we would end up with a final array of repeatedvals
ofrunlens
times.Steps: Let's number the above mentioned steps to give the prospective approach an easier perspective:
1) Initialize zeros array: What must be the length? Since we are repeating
runlens
times, the length of the zeros array must be the summation of allrunlens
.2) Find key positions/indices: Now these key positions are places along the zeros array where each element from
vals
start to repeat.Thus, for
runlens = [2,2,1,3]
, the key positions mapped onto the zeros array would be:3) Find appropriate values: The final nail to be hammered before using
cumsum
would be to put "appropriate" values into those key positions. Now, since we would be doingcumsum
soon after, if you think closely, you would need adifferentiated
version ofvalues
withdiff
, so thatcumsum
on those would bring back ourvalues
. Since these differentiated values would be placed on a zeros array at places separated by therunlens
distances, after usingcumsum
we would have eachvals
element repeatedrunlens
times as the final output.Solution Code
Here's the implementation stitching up all the above mentioned steps -
Pre-allocation Hack
As could be seen that the above listed code uses pre-allocation with zeros. Now, according to this UNDOCUMENTED MATLAB blog on faster pre-allocation, one can achieve much faster pre-allocation with -
Wrapping up: Function Code
To wrap up everything, we would have a compact function code to achieve this run-length decoding like so -
Benchmarking
Benchmarking Code
Listed next is the benchmarking code to compare runtimes and speedups for the stated
cumsum+diff
approach in this post over the othercumsum-only
based approach onMATLAB 2014B
-Associated function code for
rld_cumsum.m
:Runtime and Speedup Plots
Conclusions
The proposed approach seems to be giving us a noticeable speedup over the
cumsum-only
approach, which is about 3x!Why is this new
cumsum+diff
based approach better than the previouscumsum-only
approach?Well, the essence of the reason lies at the final step of the
cumsum-only
approach that needs to map the "cumsumed" values intovals
. In the newcumsum+diff
based approach, we are doingdiff(vals)
instead for which MATLAB is processing onlyn
elements (where n is the number of runLengths) as compared to the mapping ofsum(runLengths)
number of elements for thecumsum-only
approach and this number must be many times more thann
and therefore the noticeable speedup with this new approach!基准
针对 R2015b 进行了更新:
repelem
现在对于所有数据大小来说都是最快的。测试过的函数:
repelem
R2015a gnovice 的cumsum
解决方案中添加的函数rld_cumsum
)
cumsum
+diff
解决方案 (rld_cumsum_diff
)accumarray
解决方案 (knedlsepp5cumsumaccumarray
)来自这篇文章naive_jit_test.m
) 来测试即时编译器结果R2015 上的
test_rld.m
b:使用 R2015a 的旧时序图此处。
调查结果:
repelem
始终是最快的,大约是 2 倍。rld_cumsum_diff
始终比rld_cumsum
快。repelem
对于小数据量(小于约 300-500 个元素)来说速度最快rld_cumsum_diff
明显快于repelem< /code> 大约 5 000 个元素
repelem
比 30 000 到 300 000 个元素之间的rld_cumsum
慢rld_cumsum< /code> 与
knedlsepp5cumsumaccumarray
的性能大致相同naive_jit_test.m
具有几乎恒定的速度,与rld_cumsum
和knedlsepp5cumsumaccumarray
相当code> 对于较小的尺寸,对于大尺寸更快一点使用 R2015a 的旧速率图此处。
结论
使用
repelem
下面大约 5 000 个元素和上面的。cumsum
+diff
解决方案Benchmarks
Updated for R2015b:
repelem
now fastest for all data sizes.Tested functions:
repelem
function that was added in R2015acumsum
solution (rld_cumsum
)cumsum
+diff
solution (rld_cumsum_diff
)accumarray
solution (knedlsepp5cumsumaccumarray
) from this postnaive_jit_test.m
) to test the just-in-time compilerResults of
test_rld.m
on R2015b:Old timing plot using R2015a here.
Findings:
repelem
is always the fastest by roughly a factor of 2.rld_cumsum_diff
is consistently faster thanrld_cumsum
.repelem
is fastest for small data sizes (less than about 300-500 elements)rld_cumsum_diff
becomes significantly faster thanrepelem
around 5 000 elementsrepelem
becomes slower thanrld_cumsum
somewhere between 30 000 and 300 000 elementsrld_cumsum
has roughly the same performance asknedlsepp5cumsumaccumarray
naive_jit_test.m
has nearly constant speed and on par withrld_cumsum
andknedlsepp5cumsumaccumarray
for smaller sizes, a little faster for large sizesOld rate plot using R2015a here.
Conclusion
Use
repelem
below about 5 000 elements and the.cumsum
+diff
solution above我不知道有什么内置函数,但这里有一个解决方案:
解释:
首先创建一个与输出数组长度相同的零向量(即
b
中所有复制的总和) 。然后将它们放置在第一个元素和每个后续元素中,表示新值序列的开始位置将在输出中。然后,向量index
的累积和可用于索引a
,将每个值复制所需的次数。为了清楚起见,这就是问题中给出的
a
和b
值的各种向量的样子:编辑: 对于为了完整起见,还有另一种选择,使用 ARRAYFUN< /a>,但这似乎比上面的解决方案(向量长度最多为 10,000 个元素)的运行时间长 20-100 倍:
There's no built-in function I know of, but here's one solution:
Explanation:
A vector of zeroes is first created of the same length as the output array (i.e. the sum of all the replications in
b
). Ones are then placed in the first element and each subsequent element representing where the start of a new sequence of values will be in the output. The cumulative sum of the vectorindex
can then be used to index intoa
, replicating each value the desired number of times.For the sake of clarity, this is what the various vectors look like for the values of
a
andb
given in the question:EDIT: For the sake of completeness, there is another alternative using ARRAYFUN, but this seems to take anywhere from 20-100 times longer to run than the above solution with vectors up to 10,000 elements long:
最后(从 R2015a 开始)有一个内置且有文档记录的函数可以执行此操作,
repelem
。以下语法(其中第二个参数是向量)与此处相关:或者换句话说,“
N
中的每个元素指定重复V
中相应元素的次数。”例子:
There is finally (as of R2015a) a built-in and documented function to do this,
repelem
. The following syntax, where the second argument is a vector, is relevant here:Or put another way, "Each element of
N
specifies the number of times to repeat the corresponding element ofV
."Example:
自 R2015b 起,MATLAB 内置
repelem
中的性能问题已得到修复。我已经运行了 R2015b 中 chappjc 帖子中的test_rld.m
程序,并且repelem
现在比其他算法快大约 2 倍:The performance problems in MATLAB's built-in
repelem
have been fixed as of R2015b. I have run thetest_rld.m
program from chappjc's post in R2015b, andrepelem
is now faster than other algorithms by about a factor 2: