对于给定的长度,T
,我希望重复一个有序序列,直到达到T
。有序序列为v = (1:12)'
。
If T = 12, the output vector should be v
If T = 13, the output vector should be v and in addition the first element of v, thus [v; v(1)]
If T = 15, the output vector should be [v; v(1); v(2); v(3)]
If T = 24, the output vector should be [v; v]
最佳答案
您可以将索引与模运算符一起使用来解决此问题。唯一的复杂之处是 MATLAB 基于 1 的索引。我们生成索引 1:T
,然后使用 mod
来包装它们。由于索引基于 1,我们需要在应用 mod
之前从索引中减去 1,然后再次加 1:
v = 1:12;
T = 15;
output = v(mod(0:T-1,numel(v))+1)
关于matlab - 如何生成给定长度的重复元素向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53673807/