If a=[1 2 3], b=[10 20 30], how to create c=[10 20 20 30 30 30]?

1 vue (au cours des 30 derniers jours)
Joshua
Joshua le 14 Juil 2014
Modifié(e) : Roger Stafford le 14 Juil 2014
The b matrix i'th element is repeated the number of times the given number in 'a' matrix and placed in matix 'c'. Also if a=[0 1 2], b=[10 20 30] how to get c=[20 30 30]??

Réponse acceptée

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon le 14 Juil 2014
c = cell2mat(arrayfun(@repmat,b,ones(size(a)),a,'uni',0));
  2 commentaires
Joshua
Joshua le 14 Juil 2014
Thank you :)
Jan
Jan le 14 Juil 2014
Very nice: +1

Connectez-vous pour commenter.

Plus de réponses (2)

Roger Stafford
Roger Stafford le 14 Juil 2014
Modifié(e) : Roger Stafford le 14 Juil 2014
Another way:
t = accumarray(cumsum([1,a]).',1);
c = b(cumsum(t(1:end-1)));
  2 commentaires
Jan
Jan le 14 Juil 2014
I assume you mean "t = " instead of "t - "
Roger Stafford
Roger Stafford le 14 Juil 2014
Oops! Yes, thank you Jan.

Connectez-vous pour commenter.


Jan
Jan le 14 Juil 2014
For large array this is efficient: FEX: RunLength
c = RunLength(a, b)

Catégories

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

Translated by