Summing values for duplicate rows and columns
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ulrik William Nash
le 10 Juil 2017
Commenté : Ulrik William Nash
le 10 Juil 2017
I have a vector of rows, columns, and values that I will use to create a sparse matrix:
rows = [1 2 3 1]; columns = [1 1 2 1]; values = [10 50 25 90];
Notice the duplicates:
(1,1) 10 (1,1) 90
What I need is to eliminate (row,column) duplicates by summing the values corresponding to these duplicates for each.
The solution in the current example is:
rows = [1 2 3]; columns = [1 1 2]; values = [100 50 25];
What operation on the three initial vectors reduce them to the solution above?
0 commentaires
Réponse acceptée
Andrei Bobrov
le 10 Juil 2017
Modifié(e) : Andrei Bobrov
le 10 Juil 2017
A = accumarray([[1 2 3 1]',[1 1 2 1]'],[10 50 25 90]',[],[],[],1);
3 commentaires
Plus de réponses (1)
Walter Roberson
le 10 Juil 2017
sparse(row, columns, values) is defined to do exactly this kind of totals.
If for some reason you need the simplified outputs afterwards, you can
[r, c, v] = find() on the sparse matrix.
2 commentaires
Walter Roberson
le 10 Juil 2017
result = sparse(r, c, s);
[summary_r, summary_c, summary_s] = find(result);
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!