# count the number of unique elements

9 visualizzazioni (ultimi 30 giorni)
Elysi Cochin il 3 Giu 2022
Risposto: Walter Roberson il 3 Giu 2022
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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Walter Roberson il 3 Giu 2022
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Thermodynamics & Statistical Physics in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by