Repeat values in a vector

1 vue (au cours des 30 derniers jours)
Florian
Florian le 16 Oct 2018
Commenté : Florian le 17 Oct 2018
Assume
a1=[1 5 8];
b1=[6 4 5];
a2=[1 2 3 4 5 6 7 8];
Now I want to create a matching vector with values repeated as follows:
b2=[6 6 6 6 4 4 4 5];
Thanks in advance!
  2 commentaires
Kevin Chng
Kevin Chng le 16 Oct 2018
Have you tried it?
KSSV
KSSV le 16 Oct 2018
Modifié(e) : KSSV le 16 Oct 2018
Whats the logic behind b2? If it is a mere repeating...read about repelem and repmat.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 16 Oct 2018
b2 = b1(cumsum(ismember(a2,a1)));
  1 commentaire
Florian
Florian le 16 Oct 2018
Many thanks Andrei, also for answering my other question!

Connectez-vous pour commenter.

Plus de réponses (1)

Bruno Luong
Bruno Luong le 16 Oct 2018
Modifié(e) : Bruno Luong le 16 Oct 2018
Another method:
[~,loc] = histc(a2,a1);
b2 = b1(loc)
yet another one
b2 = b1(interp1(a1,1:length(a1),a2,'previous'))
  1 commentaire
Florian
Florian le 17 Oct 2018
Many thanks!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by