Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
aggregating data

Subject: aggregating data

From: Timothy Hilton

Date: 25 Apr, 2012 23:57:36

Message: 1 of 2


I am struggling to aggregate some data by grouping variable. I have timeseries
from a number of sensors. Each sensor has a type and a depth. I have the
types and depths as a nominal array.

I want the mean timeseries from each group of sensors (grouped by type and depth).

Here, the type is in groups( 1, : ) and the depth is in groups( 2, : ). Each
row in data is a timestamp.

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.

I've looked into grpstats, but it doesn't look like it can separate out its
grouped vector into pairs to feed to mean... I am also looking into accumarray
but can't get the indexing quite right.

Thanks in advance,
Tim

Subject: aggregating data

From: Roger Stafford

Date: 26 Apr, 2012 03:42:27

Message: 2 of 2

"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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us