From: "Daphne" <>
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$>
References: <ivpqne$cbd$> <ivq28n$5qi$> <ivqbiv$3jm$> <ivrbij$kku$> <ivrdtm$q64$> <ivrf50$t0k$> <ivrgk1$38u$> <ivrhjp$5k6$>
Reply-To: "Daphne" <>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1310805429 8218 (16 Jul 2011 08:37:09 GMT)
NNTP-Posting-Date: Sat, 16 Jul 2011 08:37:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1066798
Xref: 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!

"Matt J" wrote in message <ivrhjp$5k6$>...
> "Daphne" <> wrote in message <ivrgk1$38u$>...
> > 
> > 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.