How to find repetition of each unique array in matrix?

8 ビュー (過去 30 日間)
Moe
Moe 2014 年 11 月 27 日
回答済み: Star Strider 2014 年 11 月 27 日
Suppose I have a matrix a:
a = [100680
114678
114678
31678
31678
31678
31678
19505
19505
19505
63694
114354
114354
97157
122910
122910
92417
92417
76298];
I want to find the repetition of each unique array. For example:
100680 1
114678 2
31678 4
19505 3
63694 1
114354 2
97157 1
122910 2
92417 2
76298 1

採用された回答

Star Strider
Star Strider 2014 年 11 月 27 日
The easiest way is to use the hist function (with help from unique):
xbins = unique(a);
[counts,centres] = hist(a,xbins);
R = [centres counts']
produces:
R =
19505 3
31678 4
63694 1
76298 1
92417 2
97157 1
100680 1
114354 2
114678 2
122910 2

その他の回答 (1 件)

Guillaume
Guillaume 2014 年 11 月 27 日
Use one of the histogram functions ( histc or histcounts) with unique to generate the bins. In R2014b, you're supposed to use histcounts as histc is being deprecated.
a = [100680 114678 114678 31678 31678 31678 31678 19505 19505 19505 63694 114354 114354 97157 122910 122910 92417 92417 76298]';
reps = [unique(a) histc(a, unique(a))] %with histc
reps = [unique(a) histcounts(a, [unique(a); Inf])'] %with histcounts

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by