Path: news.mathworks.com!not-for-mail
From: "Daphne" <daphnew_too_nospam@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: average if: average values in column if values in other colum are equal
Date: Sat, 16 Jul 2011 08:37:09 +0000 (UTC)
Organization: Technion
Lines: 32
Message-ID: <ivrijl$80q$1@newscl01ah.mathworks.com>
References: <ivpqne$cbd$1@newscl01ah.mathworks.com> <ivq28n$5qi$1@newscl01ah.mathworks.com> <ivqbiv$3jm$1@newscl01ah.mathworks.com> <ivrbij$kku$1@newscl01ah.mathworks.com> <ivrdtm$q64$1@newscl01ah.mathworks.com> <ivrf50$t0k$1@newscl01ah.mathworks.com> <ivrgk1$38u$1@newscl01ah.mathworks.com> <ivrhjp$5k6$1@newscl01ah.mathworks.com>
Reply-To: "Daphne" <daphnew_too_nospam@yahoo.com>
NNTP-Posting-Host: www-04-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1310805429 8218 172.30.248.35 (16 Jul 2011 08:37:09 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 16 Jul 2011 08:37:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1066798
Xref: news.mathworks.com comp.soft-sys.matlab:736472


Got it.

Well, my final version, which also works if the unique values in column 1 are not integers (thanks for noting that Roger):

[unique_val,~,ind_unique] = unique(a(:,1));
sz = [size(unique_val,1),1];
N = accumarray(ind_unique,1,sz); % number of measurements
mean_a = accumarray(ind_unique,a(:,2),sz)./ N;  % average
std_a = sqrt(accumarray(ind_unique, ( a(:,2)-mean_a(ind_unique) ).^2 ,sz) ./ (N-1)); % sample std, use N for population std
b = [unique_val, mean_a, std_a];


Thanks a lot Matt and Roger!
Daphne



"Matt J" wrote in message <ivrhjp$5k6$1@newscl01ah.mathworks.com>...
> "Daphne" <daphnew_too_nospam@yahoo.com> wrote in message <ivrgk1$38u$1@newscl01ah.mathworks.com>...
> > 
> > Perfect!
> > 
> > I noticed a strange phenomenon, the average (and now also the std) gives me a vector that has several NaN and on the bottom the actual values I am looking for.
> > This didn't happen with the sample matrices, but does for my real data. Any ideas why?
> > Since the values themselves are fine, I just toss the NaNs and keep the values, but would be happy to know why this is happening. 
> ===================
> 
> Because in your actual data unique(a(:,1)) contains fewer values than
> 1:max(a(:,1)). ACCUMARRAY assigns zeros to these missing locations and you end up doing a 0/0 operation there.
> 
> Roger's use of UNIQUE is more neat and tidy, and more efficient I guess since 0/0 operations are more time-consuming than well-defined division operations.