Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Wan Ji
Wan Ji el 18 de Ag. de 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 comentarios
Wan Ji
Wan Ji el 21 de Ag. de 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 el 22 de Ag. de 2021
Yaaaa, I got it. Thanks so much for your support =v=

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by