Find repeated row from col1, average repeated from col2, erase repeated row

조회 수: 2 (최근 30일)
Hi, I have matrix A (7x2) where the first column has some repeated rows
A=[54.5 300; 56 250; 57 100; 56 600; 56.5 700; 57 800; 58 900]
In A, the number 56 repeats two times, the number 57 also repeats two times. [a number can be repeated a max of two times]
Whenever the number from the 1st column repeats, I need to take the average from the second column [so for 56 would be (250+600)*0.5=425] and re-express the matrix with that average in col2, and erase the repeated rows.
So the resulting matrix B would be 5x2 like this:
B= [54.5 300; 56 425; 56.5 700; 57 450; 58 900]
Any ideas please? I tried with ismember but cannot work with 0 indices. Thanks Dave

채택된 답변

the cyclist
the cyclist 2014년 10월 28일
[B1,~,j] = unique(A(:,1));
B2 = accumarray(j,A(:,2),[],@mean);
B = [B1,B2]

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by