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

1 visualización (últimos 30 días)
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

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by