本文介绍了如何在Matlab中向不同长度的向量添加重复模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题
假设我有两个任意长度的向量.让我们叫一个pattern
,另一个叫series
.现在,我想自动将重复的pattern
添加到我的series
中.
Suppose I have two vectors of arbitrary length. Lets call one pattern
and the other series
. Now I want to add my repeated pattern
to my series
in an automatic way.
通常可以假定pattern
比series
短,但是如果另一种方法也可行的话,那会很好.在这种情况下,应仅使用pattern
的前几个值.
Typically one can assume that pattern
is shorter than series
, but it would be nice if the alternate way also worked. In this case just the first few values of pattern
should be used.
示例
pattern = 1:3;
series = 1:10;
应该给
2 4 6 5 7 9 8 10 12 11
到目前为止我发现了什么?
我进行了搜索,但没有找到一种实现我想要的优雅方法.
I have searched around but did not find an elegant way to achieve what I want.
- 我找到的最简单的解决方案是使用
padarray
,但是我没有此可用的 - 我自己认为不优雅的解决方案是使用
repmat
重复图案足够的次数,然后切割末端.
- The easiest solution I found uses
padarray
, however I do not have this available - My own solution,that I don't consider to be elegant, is using
repmat
to repeat the pattern a sufficient amount of times and then cutting of the end.
推荐答案
您可以使用索引代替repmat:
You could use indexing instead of repmat:
result = series + pattern([mod(0:(numel(series) - 1), numel(pattern)) + 1]);
这篇关于如何在Matlab中向不同长度的向量添加重复模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!