MATLAB Answers

How to use the several average of same class

3 views (last 30 days)
Kong
Kong on 5 Apr 2020
Edited: Kong on 6 Apr 2020
Hello.
I have a data (90 x 2857), column 2857 is a label(class).
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?
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);

  0 Comments

Sign in to comment.

Accepted Answer

David Hill
David Hill on 5 Apr 2020
a=randi(size(values,1),5,1);%randomly choose 5 rows
avg=mean(values(a,:),2);%compute mean of the rows

  5 Comments

Show 2 older comments
Kong
Kong on 5 Apr 2020
Hello. I want to get several mean for same class. For example, there are 10 rows for "running" data. I want to ramdomly select 5 rows of 10 rows for "running" and compute the average of 5 rows. So I can get several mean rows about "running". I want to use these average.
David Hill
David Hill on 5 Apr 2020
b=randperm(length(avg),5);
for k=1:5
a(k)=mean(avg{b(k)});
end
Kong
Kong on 5 Apr 2020
Thank you so much. I got this error.
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);
a=mean(avg{randi(length(values),1)});
b=randperm(length(avg),5 );
for k=1:5
a(k)=mean(avg{b(k)});
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!