Summing values for duplicate rows and columns
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Andrei Bobrov
2017년 7월 10일
편집: Andrei Bobrov
2017년 7월 10일
A = accumarray([[1 2 3 1]',[1 1 2 1]'],[10 50 25 90]',[],[],[],1);
댓글 수: 3
추가 답변 (1개)
Walter Roberson
2017년 7월 10일
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
Walter Roberson
2017년 7월 10일
result = sparse(r, c, s);
[summary_r, summary_c, summary_s] = find(result);
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!