Path: news.mathworks.com!not-for-mail
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$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>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1310804409 5766 172.30.248.37 (16 Jul 2011 08:20:09 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 16 Jul 2011 08:20:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:736471

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