count the number of unique elements

6 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by