Sum values corresponding to a number in a vector

조회 수: 1 (최근 30일)
Sourangsu Chowdhury
Sourangsu Chowdhury 2018년 9월 24일
편집: Stephen23 2018년 9월 24일
4 1
4 10
4 2
8 20
5 12
5 4
5 8
5 6
Suppose I have two vectors, I need average of the digits in second vector corresponding to the digits in the first vector such that the result is
4 13
8 20
5 30

채택된 답변

Stephen23
Stephen23 2018년 9월 24일
편집: Stephen23 2018년 9월 24일
>> M = [4,1;4,10;4,2;8,20;5,12;5,4;5,8;5,6]
M =
4 1
4 10
4 2
8 20
5 12
5 4
5 8
5 6
>> [U,~,X] = unique(M(:,1));
>> S = accumarray(X,M(:,2),[],@sum);
>> [U,S]
ans =
4 13
5 30
8 20

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by