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:09:08 +0000 (UTC)
Organization: Xoran Technologies
Lines: 28
Message-ID: <ivrgv4$431$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>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-00-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1310803748 4193 172.30.248.45 (16 Jul 2011 08:09:08 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 16 Jul 2011 08:09:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:736470

"Matt J" wrote in message <ivrf50$t0k$1@newscl01ah.mathworks.com>...
> "Daphne" <daphnew_too_nospam@yahoo.com> wrote in message <ivrdtm$q64$1@newscl01ah.mathworks.com>...
> > 
> > A follow up question, how would you calculate the std of the means? that would be a third column in the results matrix. 
> > 
> 
> Same kind of thing...
> 
> N=accumarray(a(:,1),1);
> av=accumarray(a(:,1),a(:,2));
> avsq=accumarray(a(:,1),a(:,2).^2);
> 
> standardDevs=sqrt((avsq-av.^2)./N)
==========================

Although, it would be marginally more numerically stable to extend Roger's code as follows:


[u,i,j] = unique(a(:,1));
 sz = size(i);

N=accumarray(j,1,sz);

col1=u;
col2=accumarray(j,a(:,2),sz)./N;
col3=sqrt(accumarray(j,  ( a(:,2)-col2(j) ).^2  ,sz)./N);

 result = [col1,col2,col3];