From: Foo Fighter on 18 Sep 2009 04:10 Hi, I have an array of measurement data, e.g: mmData = [ 31 4 -21 -1 -21 10 13 3 -17 8 -22 -1 -7 -10 11 ]; each datapoint belongs to a class: mmClass = [1 3 1 2 2 2 1 2 3 2 1 3 3 3 1]; How can I get the mean of each class without using a for loop? Thanks! John
From: dpb on 21 Sep 2009 12:44 Foo Fighter wrote: > Hi, > > I have an array of measurement data, e.g: > mmData = [ 31 4 -21 -1 -21 10 13 3 -17 8 > -22 -1 -7 -10 11 ]; > > each datapoint belongs to a class: > mmClass = [1 3 1 2 2 2 1 2 3 2 > 1 3 3 3 1]; > > How can I get the mean of each class without using a for loop? Sometimes a for loop is the answer, even in ML... Interesting how this question has come up (in slightly differing guises) so frequently over last few weeks. In a thread not too long ago somebody asked in terms of a SAS "BY" clause. I seem to recollect maybe Bruno did a File Exchange submission in that regard after that question???? I didn't find the thread but eternal-september doesn't have a long retention period so it probably already dropped off the horizon... --
From: Nathan on 21 Sep 2009 14:23 On Sep 18, 1:10 am, Foo Fighter <foofig...(a)gmail.com> wrote: > Hi, > > I have an array of measurement data, e.g: > mmData = [ 31 4 -21 -1 -21 10 13 3 -17 8 > -22 -1 -7 -10 11 ]; > > each datapoint belongs to a class: > mmClass = [1 3 1 2 2 2 1 2 3 2 > 1 3 3 3 1]; > > How can I get the mean of each class without using a for loop? > > Thanks! > John This isn't a for loop, but would be nicer to do as one... classmean1 = mean(mmData(mmClass==1)) classmean2 = mean(mmData(mmClass==2)) classmean3 = mean(mmData(mmClass==3)) OR (works with any number of classes) for i=1:max(mmClass) classmean(i) = mean(mmData(mmClass==i)) end So... Like dpb said: Sometimes a for loop is the answer -Nathan
From: Bruno Luong on 21 Sep 2009 14:34 > > So... Like dpb said: Sometimes a for loop is the answer > But not the only answer: accumarray(mmClass(:), mmData, [], @mean) Bruno
|
Pages: 1 Prev: quadratic b-spline wavelet Next: Difference between Matlab2007b and Matlab2007b+ |