Element-wise array replication in Matlab

2019-01-01 02:26发布

Let's say I have a one-dimensional array:

a = [1, 2, 3];

Is there a built-in Matlab function that takes an array and an integer n and replicates each element of the array n times?

For example calling replicate(a, 3) should return [1,1,1,2,2,2,3,3,3].

Note that this is not at all the same as repmat. I can certainly implement replicate by doing repmat on each element and concatenating the result, but I am wondering if there is a built in function that is more efficient.

7条回答
谁念西风独自凉
2楼-- · 2019-01-01 03:16
>> n=3;
>> a(floor((0:size(a,2)*n-1)/n)+1)

ans =

     1     1     1     2     2     2     3     3     3
查看更多
登录 后发表回答