count the number of unique elements

조회 수: 7 (최근 30일)
Elysi Cochin
Elysi Cochin 2022년 6월 3일
답변: Walter Roberson 2022년 6월 3일
I have a matrix with 2 rows 23 columns
M = [3,2,3,3,2,3,3,2,4,3,3,3,1,1,1,3,4,4,3,5,5,3,3;1,1,1,1,1,1,1,1,1,2,2,2,3,3,4,4,4,4,5,5,6,6,6];
Now i'll take all columns where column = 1 in row2
3 2 3 3 2 3 3 2 4
1 1 1 1 1 1 1 1 1
and i need to count the number of unique elements in so much columns, and also convert into its percentage
1 = 0
2 = 3
3 = 5
4 = 1
5 = 0
percentage computation
0/9 *100
3/9 *100
5/9 *100
1/9 *100
0/9 *100
% where 9 is the number of columns with column value 1 in row2
then take all columns where column = 2 in row2
3 3 3
2 2 2
then take all columns where column = 3 in row2 and upto 6
Row1 has unique values 1 to 5, and
Row2 has unique values 1 to 6

채택된 답변

Walter Roberson
Walter Roberson 2022년 6월 3일
counts = accumarray(M([2 1],:) .', ones(size(M, 2),1))
perc = counts ./ sum(counts, 2) * 100
Rows of perc will correspond to values in row 2 of M. Columns will correspond to values in row 1 of M.
The code should be made more robust for the case where the values are not consecutive positive integers.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Thermodynamics & Statistical Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by