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

1 view (last 30 days)
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

Accepted Answer

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

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!