Repeat element of a vector n times without loop.
73 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Justin Solomon
il 28 Ago 2012
Modificato: DGM
il 2 Ago 2023
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 Commento
John
il 9 Dic 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)]
Risposta accettata
Azzi Abdelmalek
il 28 Ago 2012
Modificato: Azzi Abdelmalek
il 28 Ago 2012
n=3 ; x=(1:3)' % example
r=repmat(x,1,n)';
r=r(:)'
3 Commenti
Jan
il 29 Ago 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...
Più risposte (6)
Walter Roberson
il 28 Ago 2012
kron(x, ones(n,1))
4 Commenti
Abdelrahman Abdeltawab
il 13 Dic 2018
Modificato: Abdelrahman Abdeltawab
il 13 Dic 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 ?
Walter Roberson
il 14 Dic 2018
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
il 29 Ago 2012
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 Commenti
Walter Roberson
il 13 Set 2021
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
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
Jianshe Feng
il 3 Ott 2016
ind = [1;1;1;2;2;2;3;3;3]; x(ind)
1 Commento
Walter Roberson
il 7 Apr 2017
Ah, but how do you construct the ind vector for general length n repetitions ?
Vedere anche
Categorie
Scopri di più su Logical (Boolean) Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!