Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Wan Ji
Wan Ji il 18 Ago 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 Commenti
Wan Ji
Wan Ji il 21 Ago 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by