Repeat element of a vector n times without loop.

조회 수: 109 (최근 30일)
Justin Solomon
Justin Solomon 2012년 8월 28일
편집: DGM 2023년 8월 2일
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
John
John 2015년 12월 9일
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)]

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 28일
편집: Azzi Abdelmalek 2012년 8월 28일
n=3 ; x=(1:3)' % example
r=repmat(x,1,n)';
r=r(:)'
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 29일
%you mean
r = repmat(x', n, 1)
Jan
Jan 2012년 8월 29일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (6개)

jack
jack 2015년 11월 23일
I would use
repelem(X,3,1)
  댓글 수: 3
Arif Billah
Arif Billah 2023년 8월 1일
This should be chosen as the best 'correct' answer, thanks!
DGM
DGM 2023년 8월 2일
편집: DGM 2023년 8월 2일
This is probably the more accepted answer today (hence the upvotes), but repelem() was not available until after the question was originally answered (R2015a).

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2012년 8월 28일
kron(x, ones(n,1))
  댓글 수: 4
Abdelrahman Abdeltawab
Abdelrahman Abdeltawab 2018년 12월 13일
편집: Abdelrahman Abdeltawab 2018년 12월 13일
Dear Walter Roberson,
why you did not use outer product and you chosen kronecker ( just curious ) because the guy's question was having vectors ?
Walter Roberson
Walter Roberson 2018년 12월 14일
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

댓글을 달려면 로그인하십시오.


Kevin Moerman
Kevin Moerman 2012년 8월 29일
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
Vinay Chakravarthi
Vinay Chakravarthi 2015년 1월 20일
Thx Man..
Walter Roberson
Walter Roberson 2021년 9월 13일
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

댓글을 달려면 로그인하십시오.


Justin Solomon
Justin Solomon 2012년 8월 28일
Thanks guys, these all work perfectly!

Jianshe Feng
Jianshe Feng 2016년 10월 3일
y = repmat(x,1,3); y = transpose(y); y = y(:);

Jianshe Feng
Jianshe Feng 2016년 10월 3일
ind = [1;1;1;2;2;2;3;3;3]; x(ind)
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 4월 7일
Ah, but how do you construct the ind vector for general length n repetitions ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by