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.

답변 (1개)

Star Strider
Star Strider 2017년 9월 7일

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.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2017년 9월 7일

답변:

2017년 9월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by