필터 지우기
필터 지우기

Mean of classified data

조회 수: 1 (최근 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