Count the number of equal and different occurrences in an array

3 vues (au cours des 30 derniers jours)
Good Morning.
In case I have an array with 200 or more rows. In this matrix, I have 3 columns but I need to count the number of equal and different occurrences.
Example:
column 1 column 2 column 3
1 0 0
2 0 0
3 1 1
4 0 1
5 1 1
6 1 0
...
and so on. What I want is to compare column 2 with column 3 and return the number of occurrences as I say below.
What I intend is:
1 1 -> appears 2 times (in the example)
0 1 -> appears 1 time and so on (in the example)
1 0 -> appears 1 time and so on (in the example)
0 0 -> appears 2 times and so on (in the example)
Only this replicated by 200 or more lines..
Any idea?
I think it's something easy but I can't think of anything
Thanks

Réponse acceptée

Star Strider
Star Strider le 1 Avr 2022
Use unique on the last two columns, then accumarray to sum the occurrences —
A = [1 0 0
2 0 0
3 1 1
4 0 1
5 1 1
6 1 0];
[Au23,ia,ix] = unique(A(:,[2 3]), 'rows');
Tally = accumarray(ix,1);
Result = [Au23, Tally]
Result = 4×3
0 0 2 0 1 1 1 0 1 1 1 2
.
  2 commentaires
PEDRO ALEXANDRE Fernandes
Thank you so much!
Wonderfull day for you
Star Strider
Star Strider le 1 Avr 2022
As always, my pleasure!
You, too!
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by