0
votes

I have a data set with n samples d features represented by a n*d matrix. The corresponding label is a n*1 vector. How can I compute each intraclass standard deviation without loops in matlab?

For example:

Samples

 5     1     1     1     4
 5     2     5     3     1
 1     3     5     5     5
 5     5     3     4     5
 4     5     5     5     4

Label:

2
1
1
2
2

How can I compute class 1 and class 2's standard deviation?

1

1 Answers

4
votes

Using accumarray, you can calculate the standard deviations like this:

stdev = cell2mat(accumarray(label,(1:length(label))',[],@(x){std(samples(x,:))}));

If you have the Statistics toolbox, you can use grpstats instead:

stdev = grpstats(samples,label,'std')