Sum every element in matrix per group

조회 수: 3 (최근 30일)
Joes de Jonge
Joes de Jonge 2021년 6월 16일
답변: SALAH ALRABEEI 2021년 6월 16일
Let A = [1, 4; 5, 6] and b = [1, 1, 1, 2]. A is here the matrix with the values and b the matrix with the group labels. I would like to sum every item in A according to their label such that I would get something back like c = [10, 6] where thus the labels of b correspond to the indices of c.
How can this be done in MatLab? I already saw accumarray is a logical function to use but it doesn't work on values that aren't vectors.

답변 (1개)

SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 16일
d = unique(b);
c = [];for i=1:length(d)
c=[c,sum(a(b==d(i)))];
end

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by