Count the number of equal and different occurrences in an array

3 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2022-4-1
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
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by