根据计数按元素复制数组
我的问题与此类似,但我想复制每个元素根据相同大小的第二个数组中指定的计数。
举个例子,假设我有一个数组 v = [3 1 9 4]
,我想使用 rep = [2 3 1 5]
来复制第一个元素2次,第二次3次,以此类推,得到[3 3 1 1 1 9 4 4 4 4 4]
。
到目前为止,我正在使用一个简单的循环来完成工作。这就是我开始的地方:
vv = [];
for i=1:numel(v)
vv = [vv repmat(v(i),1,rep(i))];
end
我设法通过预分配空间来改进:
vv = zeros(1,sum(rep));
c = cumsum([1 rep]);
for i=1:numel(v)
vv(c(i):c(i)+rep(i)-1) = repmat(v(i),1,rep(i));
end
但是我仍然觉得必须有一种更聪明的方法来做到这一点......谢谢
My question is similar to this one, but I would like to replicate each element according to a count specified in a second array of the same size.
An example of this, say I had an array v = [3 1 9 4]
, I want to use rep = [2 3 1 5]
to replicate the first element 2 times, the second three times, and so on to get [3 3 1 1 1 9 4 4 4 4 4]
.
So far I'm using a simple loop to get the job done. This is what I started with:
vv = [];
for i=1:numel(v)
vv = [vv repmat(v(i),1,rep(i))];
end
I managed to improve by preallocating space:
vv = zeros(1,sum(rep));
c = cumsum([1 rep]);
for i=1:numel(v)
vv(c(i):c(i)+rep(i)-1) = repmat(v(i),1,rep(i));
end
However I still feel there has to be a more clever way to do this... Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我喜欢完成此任务的一种方法:
首先创建一个由零组成的索引向量,其长度与所有值的最终计数相同。通过对删除最后一个元素并在开头放置 1 的
rep
向量执行累积和,我得到了index
的索引向量,显示了复制组的位置值将开始。这些点都用 1 标记。当对index
执行累积和时,我得到一个最终索引向量,我可以使用它索引到v
以创建异构复制值的向量。Here's one way I like to accomplish this:
This works by first creating an index vector of zeroes the same length as the final count of all the values. By performing a cumulative sum of the
rep
vector with the last element removed and a 1 placed at the start, I get a vector of indices intoindex
showing where the groups of replicated values will begin. These points are marked with ones. When a cumulative sum is performed onindex
, I get a final index vector that I can use to index intov
to create the vector of heterogeneously-replicated values.要添加到可能的解决方案列表中,请考虑以下一个:
这比 新手..
To add to the list of possible solutions, consider this one:
This is much slower than the one by gnovice..
您想要做的是游程解码。高级可靠/矢量化实用程序是 FEX 提交
rude()
:结果
请注意,此函数也执行相反的操作,即游程编码向量,或者换句话说,返回
值
和相应的>计数
。What you are trying to do is to run-length decode. A high level reliable/vectorized utility is the FEX submission
rude()
:the result
Note that this function performs the opposite operation as well, i.e. run-length encodes a vector or in other words returns
values
and the correspondingcounts
.如果
rep
数组中存在零,则可以使用 Accumarray 函数使代码工作。这与 gnovice 的解决方案类似,只是索引被累加而不是分配给 1。允许跳过一些索引(下例中的 3 和 6)并从输出中删除相应的元素。
accumarray
function can be used to make the code work if zeros exit inrep
arrayThis works similar to solution of gnovice, except that indices are accumulated instead being assigned to 1. This allows to skip some indices (3 and 6 in the example below) and remove corresponding elements from the output.