Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by