Count the number of equal and different occurrences in an array

3 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 1 Apr 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
.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!