Count the adjacent same elements in a vector
Afficher commentaires plus anciens
Hello! Assume we have a vector of A=[1 1 1 2 2 3 3 3 3 3 1 1]. A has three 1, two 2, five 3 and two 1.
I need to create a vector, such as B=[3 2 5 2] and their corresponding value C=[1 2 3 1].
Maybe i should use a struct, not sure. Glad for some help :/
thx.
Réponse acceptée
Plus de réponses (1)
Jos (10584)
le 1 Juil 2019
This is call run-length encoding, for which you can find excellent function on the File exchange. For instance, [shameless self promotion ;-) ], this one: https://uk.mathworks.com/matlabcentral/fileexchange/56131-runindex
A = [1 1 1 2 2 3 3 3 3 3 1 1]
[~, RLE] = runindex(A)
B = RLE(:,3)
C = RLE(:,1)
1 commentaire
ayça kepçe
le 9 Juil 2019
Catégories
En savoir plus sur Data Types dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!