Finding similar elements in a single matrix
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Md. Nazrul Islam Siddique
il 2 Dic 2022
Commentato: Star Strider
il 2 Dic 2022
I have one matrix,
x = [
2 0 0
3 3 0
4 4 0
5 5 5
6 6 6]
How can I find the common elements between columns in the matrix and count them? for example, here 3 and 4 repeated two times.
Risposta accettata
Star Strider
il 2 Dic 2022
x = [2 0 0
3 3 0
4 4 0
5 5 5
6 6 6];
[Aunique,~,idx] = unique(x(:));
Counts = accumarray(idx, 1);
Result = table(Aunique, Counts)
.
9 Commenti
Star Strider
il 2 Dic 2022
Just sum the ‘Count’ results —
x = [1 2 3
4 5 6];
y = [1 5 7
2 5 8];
xy = [x; y]
xy = xy(xy~=0);
[XYunique,~,idx] = unique(xy(:));
Counts = accumarray(idx, 1);
Result = table(XYunique, Counts)
TotalCounts = sum(Counts)
If the matrices are not conformable to concatenation (they are conformable here), then reshape both to be column vectors using the column vector operator ‘(:)’ and then vertically concatenate those. Then use unique and accumarray.
.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Preprocessing Data 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!