How to group by in matlab?

조회 수: 13 (최근 30일)
chen xie
chen xie 2021년 5월 8일
편집: Cris LaPierre 2021년 5월 8일
There is a matrix: [1 3 1;1 3 2;1 3 3;1 3 4;3 1 2;3 1 3]
i want to group by first two columns, and sum the third column.
idealy result is [1 3 10;3 1 5]
  댓글 수: 1
chen xie
chen xie 2021년 5월 8일
The first two columns of matrix are considered indexes. This is only an example, my dataset is very large and don't know which indexes are duplicated,I want to sum the third column with repeated indexes to form the final matrix.

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 5월 8일
편집: Cris LaPierre 2021년 5월 8일
Perhaps with groupsummary?
A=[1 3 1;1 3 2;1 3 3;1 3 4;3 1 2;3 1 3];
[B,BG]=groupsummary(A(:,3),{A(:,1),A(:,2)},'sum');
C = [cell2mat(BG),B]
C = 2×3
1 3 10 3 1 5

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by