Count the number of the same rows in array

I have array A and I want to count number of occurence the same rows in array
A=[ 44444; 66666; 44444; 88888; 44444; 66666]
Result should be like output.
output:
44444 3
66666 2
88888 1
As you can see row 44444 occured 3 times in array A,66666 2 times and 88888 one time.
The best will be use only matlab function instead of loops.

Answers (1)

Try this:
A=[ 44444; 66666; 44444; 88888; 44444; 66666];
[Au,~,ic] = unique(A, 'stable');
tally = accumarray(ic, 1);
Result = [Au tally]
Result =
44444 3
66666 2
88888 1
The unique function finds the unique value and returns a vector (here ‘ic’) that are the indices of the unique values in ‘A’. The accumarray function counts and produces as output a vector of the number of occurrences of the unique values. The ‘Result’ matrix horizontally concatenates the unique values and the ‘tally’ vector.

Categories

Asked:

on 7 Sep 2017

Answered:

on 7 Sep 2017

Community Treasure Hunt

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

Start Hunting!