Fast creation of vector [0 0 1 1 2 2 3 3... n n]
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi all,
like mentioned in the title, is there a fast way of creating a vector with repeating digits?
Thanks!
Simon
0 commentaires
Réponse acceptée
per isakson
le 30 Juin 2014
Modifié(e) : per isakson
le 30 Juin 2014
Test
>> reshape( cat( 1, [0:n], [0:n] ), 1, [] )
ans =
0 0 1 1 2 2 3 3 4 4 5 5
and
>> reshape( repmat( [0:n], 2,1 ), 1, [] )
1 commentaire
Plus de réponses (4)
Jos (10584)
le 30 Juin 2014
n = 10 % max value
k = 3 % number of repetitions
V = floor((0:k*(n+1)-1)/k)
3 commentaires
Jos (10584)
le 1 Juil 2014
Reshaping does not take a lot of time. Is it also faster for larger values of k?
per isakson
le 1 Juil 2014
Modifié(e) : per isakson
le 3 Juil 2014
Is this solution immune to floating point errors?
Danilo NASCIMENTO
le 30 Juin 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
0 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!