3 views (last 30 days)

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 = readtable("outfile.csv");

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

Ameer Hamza
on 2 Apr 2020

Edited: Ameer Hamza
on 2 Apr 2020

Try this

data = csvread('outfile.csv');

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.

Ameer Hamza
on 3 Apr 2020

@Kong, your question is not clear. Do you want to apply softmax function to all the distance values

data = csvread('outfile.csv');

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);

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/514912-how-to-use-calculate-the-several-means-of-same-class#comment_822099

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/514912-how-to-use-calculate-the-several-means-of-same-class#comment_822099

Sign in to comment.