How do I locate the most repeated elements in an array/vector?

11 vues (au cours des 30 derniers jours)
Sebastian
Sebastian le 18 Juil 2023
Commenté : Sebastian le 23 Juil 2023
For example, we have an array
a = [1 1 1 0 1 1 0 2 2 2]
Given an input of this array, how can I find the number(s) in the array that is/are repeated consecutively most often? i.e. return:
[1 2]
New to matlab and not sure how to do this.

Réponses (2)

Bruno Luong
Bruno Luong le 18 Juil 2023
Modifié(e) : Bruno Luong le 18 Juil 2023
a = [1 1 1 0 1 1 0 2 2 2];
i = find([true diff(a)~=0 true]);
n = diff(i);
j = find(n == max(n));
b = a(i(j))
b = 1×2
1 2
  1 commentaire
Sebastian
Sebastian le 23 Juil 2023
This seems to work really well, and I've taken the time to really understand it as a new user of MATLAB.
However, I'm getting some sort of horzcat error in Cody's test suite. In my own live script I get my expected answer and I don't get any sort of error. Know what might be going on?

Connectez-vous pour commenter.


Matt J
Matt J le 18 Juil 2023
Modifié(e) : Matt J le 18 Juil 2023

Catégories

En savoir plus sur Data Distribution Plots 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