Repeat element of a vector n times without loop.

Say I have a column vector x=[a;b;c]. I want to repeat each element n times to make a long length(x)*n vector. For example, for n=3, the answer would be:
ans=
a
a
a
b
b
b
c
c
c
Can anyone think of an elegant way to do this without looping?
Thanks,
Justin

1 Kommentar

John
John am 9 Dez. 2015
U can use repmat it not exactly elegant but it will do the job
x=[a;b;c]; n=3;
newx = [repmat(x(1),n,1);repmat(x(2),n,1);repmat(x(3),n,1)]

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 28 Aug. 2012
Bearbeitet: Azzi Abdelmalek am 28 Aug. 2012

17 Stimmen

n=3 ; x=(1:3)' % example
r=repmat(x,1,n)';
r=r(:)'

3 Kommentare

Jan
Jan am 29 Aug. 2012
Bearbeitet: Jan am 29 Aug. 2012
This is more efficient than KRON. Small improvement: r = repmat(x, n, 1); without tranposing.
%you mean
r = repmat(x', n, 1)
Jan
Jan am 29 Aug. 2012
I guess, you are right. repmat(1:3, 1, 2) = [1,2,3,1,2,3] but the OP wants [1,1,2,2,3,3]. Then r = repmat(1:3, 2, 1); r = r(:) avoid the expensive transposition of the matrix. Well, I admit that even reading this message will waste more time then millions of matrix transpositions will cost...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (6)

jack
jack am 23 Nov. 2015

19 Stimmen

I would use
repelem(X,3,1)

3 Kommentare

Yuzhen Lu
Yuzhen Lu am 18 Feb. 2021
Very neat answer!
This should be chosen as the best 'correct' answer, thanks!
DGM
DGM am 2 Aug. 2023
Bearbeitet: DGM am 2 Aug. 2023
This is probably the more accepted answer today (hence the upvotes), but repelem() was not available until after the question was originally answered (R2015a).

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 28 Aug. 2012

4 Stimmen

kron(x, ones(n,1))

4 Kommentare

Thx man.......
Very nice man
Abdelrahman Abdeltawab
Abdelrahman Abdeltawab am 13 Dez. 2018
Bearbeitet: Abdelrahman Abdeltawab am 13 Dez. 2018
Dear Walter Roberson,
why you did not use outer product and you chosen kronecker ( just curious ) because the guy's question was having vectors ?
The * matrix multiplication operator cannot by itself repeat elements. You would need something like
(x.' * repmat(eye(length(x)), 1, n)).'
if you wanted to use the * operator to duplicate elements -- forcing you to call upon repmat() to duplicate elements.
Using the kronecker is a known idiom for duplicating data. It can be used for non-vectors too.
>> kron([1 2;3 4], ones(3,1))
ans =
1 2
1 2
1 2
3 4
3 4
3 4

Melden Sie sich an, um zu kommentieren.

Kevin Moerman
Kevin Moerman am 29 Aug. 2012

2 Stimmen

There is several others ways of doing it which in some cases are more efficient. Have a look at what the size of your vector is and compare the methods. Below I compare speeds and it appears that on my computer the third and fourth methods are mostly faster for large arrays.
n=100000; x=1:3;
a=zeros(n,numel(x)); b=a; c=a; d=a; %memory allocation
tic; a=repmat(x, n, 1); t1=toc; %Repmat method
tic; b=kron(x, ones(n,1)); t2=toc; %kron method
tic; c=x(ones(1,n),:); t3=toc; %indexing method
tic; d=ones(n,1)*x; t4=toc; %multiplication method
Kevin

2 Kommentare

Thx Man..
format long g
n=100000; x=1:3;
a=zeros(n,numel(x)); b=a; c=a; d=a; %memory allocation
tic; a=repmat(x, n, 1); t1=toc %Repmat method
t1 =
0.000543
tic; b=kron(x, ones(n,1)); t2=toc %kron method
t2 =
0.006106
tic; c=x(ones(1,n),:); t3=toc %indexing method
t3 =
0.002276
tic; d=ones(n,1)*x; t4=toc %multiplication method
t4 =
0.001798

Melden Sie sich an, um zu kommentieren.

Justin Solomon
Justin Solomon am 28 Aug. 2012

0 Stimmen

Thanks guys, these all work perfectly!
Jianshe Feng
Jianshe Feng am 3 Okt. 2016

0 Stimmen

y = repmat(x,1,3); y = transpose(y); y = y(:);
Jianshe Feng
Jianshe Feng am 3 Okt. 2016

0 Stimmen

ind = [1;1;1;2;2;2;3;3;3]; x(ind)

1 Kommentar

Ah, but how do you construct the ind vector for general length n repetitions ?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by