Count the number of equal and different occurrences in an array

3 ビュー (過去 30 日間)
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes 2022 年 4 月 1 日
コメント済み: Star Strider 2022 年 4 月 1 日
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
.
  2 件のコメント
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes 2022 年 4 月 1 日
Thank you so much!
Wonderfull day for you
Star Strider
Star Strider 2022 年 4 月 1 日
As always, my pleasure!
You, too!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by