From: <HIDDEN>
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:20:09 +0000 (UTC)
Organization: Xoran Technologies
Lines: 13
Message-ID: <ivrhjp$5k6$>
References: <ivpqne$cbd$> <ivq28n$5qi$> <ivqbiv$3jm$> <ivrbij$kku$> <ivrdtm$q64$> <ivrf50$t0k$> <ivrgk1$38u$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1310804409 5766 (16 Jul 2011 08:20:09 GMT)
NNTP-Posting-Date: Sat, 16 Jul 2011 08:20:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: comp.soft-sys.matlab:736471

"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.