# How to use calculate the several means of same class

3 views (last 30 days)
Kong on 2 Apr 2020
Edited: Kong on 6 Apr 2020
Hello.
I have a data (90 x 2857), column 2857 is a label(class).
I'm using table function.
I want to read rows of the same class from table, select N rows, calculate the average.
Could you giva an idea to make the code?
data.Properties.VariableNames{end} = 'label';
data{:,1:end-1} = normalize(data{:,1:end-1},1); % I belive this should be "1"
data = sortrows(data,'label'); % The data are sorted by the lables

#### 1 Comment

Kong on 6 Apr 2020
I did't find how to select rows for same class randomly.
Can I get some ideas to make a code?

Ameer Hamza on 2 Apr 2020
Edited: Ameer Hamza on 2 Apr 2020
Try this
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) mean(x,1), values, labels+1);
The avg matrix is 10x2856, each row corresponds to mean value of one class.

Kong on 3 Apr 2020
Thank you so much. After calculating the mean of each class, I want to select sample row to compute Euclidean distance between the row and the mean of classes.
For example, I have 10 classes. The results are 10 Euclidean distance. I want to use softmax after the results of Euclidean distance and finally apply loss function.
Ameer Hamza on 3 Apr 2020
@Kong, your question is not clear. Do you want to apply softmax function to all the distance values
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);
grps = splitapply(@(x) {x}, values, labels+1);
Distance = cellfun(@(x,y) {pdist2(x,y)}, grps, avg);
dlY = cellfun(@(x) {softmax(x)}, Distance);
Kong on 4 Apr 2020
Thank you so much.
I want to select 5 rows of the same classes randomly and compute the mean of each 5 rows.
(10 Combination 5)
So I want to get all possible combinations' average values of the same classes.
In this code, I computed all rows of the same class and got one average value.
Could you explain how to fix the code?
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);