Fast creation of vector [0 0 1 1 2 2 3 3... n n]
Mostra commenti meno recenti
Hi all,
like mentioned in the title, is there a fast way of creating a vector with repeating digits?
Thanks!
Simon
Risposta accettata
Più risposte (4)
Andrei Bobrov
il 1 Lug 2014
k = [1;1]*(0:n);
out = k(:)';
1 Commento
Paul Safier
il 8 Dic 2022
Perfect.
Jos (10584)
il 30 Giu 2014
n = 10 % max value
k = 3 % number of repetitions
V = floor((0:k*(n+1)-1)/k)
3 Commenti
Jos (10584)
il 1 Lug 2014
Reshaping does not take a lot of time. Is it also faster for larger values of k?
per isakson
il 1 Lug 2014
Modificato: per isakson
il 3 Lug 2014
Is this solution immune to floating point errors?
n = 5;
x1 = repelem(0:n, 2)
1 Commento
Paul Safier
il 8 Dic 2022
Oh wow, very nice. I'd never heard of the repelem function. Thanks.
Danilo NASCIMENTO
il 30 Giu 2014
V=zeros(1,20);
k=0;
i=1;
while i<20
V(i)=k;
V(i+1)=k;
k=k+1;
i=i+2;
end
V
Categorie
Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!