Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: aggregating data
Date: Thu, 26 Apr 2012 03:42:27 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 25
Message-ID: <jnag73$2gd$1@newscl01ah.mathworks.com>
References: <jna31g$cq1$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1335411747 2573 172.30.248.47 (26 Apr 2012 03:42:27 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 26 Apr 2012 03:42:27 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:765943

"Timothy Hilton" wrote in message <jna31g$cq1$1@newscl01ah.mathworks.com>...
> groups = nominal( {'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'; ...
>                    '1', '2', '1', '2', '1', '2', '1', '2' } );
> data = rand( 10, 8 );
> 
> I need the row-wise mean of each combination of type/depth I could solve this
> example with
> 
> [ mean( data( :, [1, 3] ), 2 ), ...
>   mean( data( :, [2, 4] ), 2 ), ...
>   mean( data( :, [5, 7] ), 2 ), ...
>   mean( data( :, [6, 8] ), 2 ) ];
> 
> I have a number of sites where the number of types and depths varies as well as
> their relative column positions, so I'm trying to figure out a vectorized
> solution to identify groups and aggregate.  
- - - - - - - - -
  Try this:

 [~,~,p] = unique(groups.','rows');
 [I,J] = ndgrid(1:size(data,1),p);
 S = [I(:),J(:)];
 A = accumarray(S,data(:))./accumarray(S,1);

Roger Stafford