Mean of classified data

2 次查看(过去 30 天)
Juanjo Gomez
Juanjo Gomez 2017-5-2
回答: Mann Baidi 2023-12-7
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.

回答(1 个)

Mann Baidi
Mann Baidi 2023-12-7
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!

类别

Help CenterFile Exchange 中查找有关 Classification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by