Effacer les filtres
Effacer les filtres

How to avoid for-loop here; repmat

2 vues (au cours des 30 derniers jours)
Pete sherer
Pete sherer le 17 Sep 2018
Commenté : Star Strider le 17 Sep 2018
Hi Could anyone help suggest a faster way to construct this vector?
xval = [1 2 7 9];
numX = [1 4 2 3];
% I want final output to be [1 2 2 2 2 7 7 9 9 9];
fullX = [];
for runi=1:length( xval)
tval = xval( runi);
n = numX( runi);
if n > 0
fullX= [ fullX; repmat( tval, n, 1)];
end
end %

Réponse acceptée

Star Strider
Star Strider le 17 Sep 2018
If you have R2015a or later, use the repelem (link) function:
fullX = repelem(xval, numX)
  2 commentaires
Pete sherer
Pete sherer le 17 Sep 2018
Thank you verymuch
Star Strider
Star Strider le 17 Sep 2018
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices 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