フィルターのクリア

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 Center および File ExchangeClassification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by