Count frequency of each line in an array using Matlab
I have this huge array. I have allocated the unique rows in a separate array. Now I would like to create a vector that will store the occurrences of each unique string. How could I do this? Tried using histc. I found about tabulate
, but only works on vectors.
x=[62 29 64
63 32 61
63 32 61
63 32 61
63 31 62
62 29 64
62 29 64
65 29 60
62 29 64
63 32 61
63 32 61
63 29 62
63 32 61
62 29 64
];
uA=unique(x)
[row, count] = histc(x,unique(x,'rows'))
I am getting the following error: Edge vector must be monotonically non-decreasing.
Also ran into this error on several other attempts.
source to share
Use unique
this way -
[unique_rows,~,ind] = unique(x,'rows')
counts = histc(ind,unique(ind))
unqiue_rows
and counts
will be the results you output.
With your data, it gives -
unique_rows =
62 29 64
63 29 62
63 31 62
63 32 61
65 29 60
counts =
5
1
1
6
1
Bonus: You can improve performance by avoiding the second use unique
this way -
counts = histc(ind,1:max(ind));
source to share