Mean of a repeating numbers corresponding values

2 visualizaciones (últimos 30 días)
Sandeep Nair
Sandeep Nair el 24 de Mzo. de 2022
Comentada: Image Analyst el 24 de Mzo. de 2022
I have a matrix like [ 10 40; 10 60 ;10 90; 14 10; 14 20; 14 30; 14 40] i want to calculate the mean of the repeating numbers corresponding values like repeating 10 corresponding values are 40 60 90 so the mean is 190/3 and similarly the 14 corresponding values are 10 ,20,30,40 so the mean is 100/4. How can i do this ?

Respuesta aceptada

Simon Chan
Simon Chan el 24 de Mzo. de 2022
Use function groupsummary:
A = [ 10 40; 10 60 ;10 90; 14 10; 14 20; 14 30; 14 40];
T = table(A(:,1),A(:,2),'VariableName',{'Item','Value'});
S = groupsummary(T,'Item','mean')
S = 2×3 table
Item GroupCount mean_Value ____ __________ __________ 10 3 63.333 14 4 25
  4 comentarios
Simon Chan
Simon Chan el 24 de Mzo. de 2022
A = [ 10 40; 10 60 ;10 90; 14 10; 14 20; 14 30; 14 40];
Item = A(:,1);
Value = A(:,2);
[UItem,~] = unique(Item);
meanValue = arrayfun(@(x) mean(Value(ismember(Item,x))),UItem);
result = [UItem, meanValue]
result = 2×2
10.0000 63.3333 14.0000 25.0000
Image Analyst
Image Analyst el 24 de Mzo. de 2022
@Sandeep Nair you can use splitapply(), introduced in 2015b.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by