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

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by