Effacer les filtres
Effacer les filtres

How can I figure out which element in the array is repeated and how many times?

2 vues (au cours des 30 derniers jours)
Hi every one, I have a problem with matrix.
Code:
A = {''A'', "B", ''C''; "D", "E", "F"; ''A'', "G", "H"; ''C'', "Y", "C"};
How can I figure out which element in the array is repeated and how many times?
Thanks and Best Regards,
Kiet Vo

Réponse acceptée

Wan Ji
Wan Ji le 18 Août 2021
Hi, friend, using histcounts, things will be simple
A = {'A', 'B', 'C'; 'D', 'E', 'F'; 'A', 'G', 'H'; 'C', 'Y', 'C'};
A = categorical (A);
[counts, val] = histcounts(A)
Results become
counts =
2 1 3 1 1 1 1 1 1
val =
1×9 cell 数组
{'A'} {'B'} {'C'} {'D'} {'E'} {'F'} {'G'} {'H'} {'Y'}
  8 commentaires
Wan Ji
Wan Ji le 21 Août 2021
That's Simple to solve
function [x, characters, counts]= another_function(A)
A = categorical (A);
[counts, val] = histcounts(A);
q = counts>=2;
counts = counts(q);
characters = val(q);
x = 2*counts;
end
Võ Kiet
Võ Kiet le 22 Août 2021
Yaaaa, I got it. Thanks so much for your support =v=

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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