Effacer les filtres
Effacer les filtres

How to Create array with repeating values of another array

5 vues (au cours des 30 derniers jours)
David Quilligan
David Quilligan le 28 Jan 2016
Commenté : David Quilligan le 28 Jan 2016
Hi I have an array say x = [1 2 3 4 5] generated from previous code. I want to then create an array y = [1 1 2 2 3 3 4 4 5 5] what is the easiest way to accomplish this?

Réponses (2)

Brendan Hamm
Brendan Hamm le 28 Jan 2016
x = [1 2 3 4 5];
y = repelem(x,2)
y =
1 1 2 2 3 3 4 4 5 5

Titus Edelhofer
Titus Edelhofer le 28 Jan 2016
Hi David,
use repmat and linear indexing:
x = [1 2 3 4 5];
% repeat two times:
xx = repmat(x, 2, 1);
% and access all elements
xRep = xx(:)'
xRep =
1 1 2 2 3 3 4 4 5 5
Titus

Catégories

En savoir plus sur Matrix Indexing 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!

Translated by