Borrar filtros
Borrar filtros

Mean of classified data

1 visualización (últimos 30 días)
Juanjo Gomez
Juanjo Gomez el 2 de Mayo de 2017
Respondida: Mann Baidi el 7 de Dic. de 2023
I have a matrix X where each row represents an observation. For example:
X =
1 3
2 5
5 7
and a vector column classes that represents, for each observation in X, which class it belongs to, for example
classes=
1
2
1
That means observation (1,3) and (5,7) are classified as 1 and observation (2,5) is classified as 2.
I'm looking for a efficiently method to calculate the mean of each class, so in this example, I get (3,5) mean for class 1 and (2,5) mean for class 2.

Respuestas (1)

Mann Baidi
Mann Baidi el 7 de Dic. de 2023
Hi Juanjo,
I understand you would like to perform “mean” operations on a classified 2D array.
For this you can use the “accumarray” function in MATLAB to perform arithmetic operations on classified arrays.
You can resolve your query using “accumarray” as follows:
X=[1 3;2 5; 5 7;10 15;64 67];
classes=[1 3 1 2 2]';
res=[accumarray(classes,X(:,1),[],@mean) accumarray(classes,X(:,2),[],@mean)]
res = 3×2
3 5 37 41 2 5
You can refer to the “accumarray” documentation using the following link.
Hope this will help!

Community Treasure Hunt

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

Start Hunting!

Translated by