Thread Subject:
grpstats(): how to pass multiple variables to function handle?

Subject: grpstats(): how to pass multiple variables to function handle?

From: Hoi Wong

Date: 29 Nov, 2011 02:05:10

Message: 1 of 5

I'm looking to reduce two variables (X, Y) of a grouped dataset object into one variable by a function like corr(X,Y), but I cannot find a good way to enter that as a function handle in grpstats() as they only accept one input.

Is there a neat way to achieve this without concatenating two variables into one matrix? My dataset is pretty large and dense, so I don't want to unnecessarily double the amount of required memory.

Thanks.

Subject: grpstats(): how to pass multiple variables to function handle?

From: ade77

Date: 29 Nov, 2011 15:18:08

Message: 2 of 5

 I'm looking to reduce two variables (X, Y) of a grouped dataset object into one variable by a function like corr(X,Y), but I cannot find a good way to enter that as a function handle in grpstats() as they only accept one input.


*********************************************************************
to use grpstats(), u will need a measurement, and say your groups are X, Y.
a = grpstats(meas, group, @(c) corr(c))
where your group will be X, and Y in one length, it could be cell array or logical or nummeric etc. There is an example in the statistics toolbox on grouped data.

btw, what kind of variables are ur X and Y.

*******************************************************************



> Is there a neat way to achieve this without concatenating two variables into one matrix? My dataset is pretty large and dense, so I don't want to unnecessarily double the amount of required memory.
*******************************************************************
a = grpstats(meas, [X;Y], @(c) corr(c)) ------ the variables X, Y , are already in memory, u are not using additional memory
*******************************************************************

Subject: grpstats(): how to pass multiple variables to function handle?

From: ade77

Date: 29 Nov, 2011 15:22:08

Message: 3 of 5

a = grpstats(meas, group, @corr) ------ does it directly

Subject: grpstats(): how to pass multiple variables to function handle?

From: Peter Perkins

Date: 29 Nov, 2011 19:43:33

Message: 4 of 5

On 11/28/2011 9:05 PM, Hoi Wong wrote:
> I'm looking to reduce two variables (X, Y) of a grouped dataset object
> into one variable by a function like corr(X,Y), but I cannot find a good
> way to enter that as a function handle in grpstats() as they only accept
> one input.
> Is there a neat way to achieve this without concatenating two variables
> into one matrix? My dataset is pretty large and dense, so I don't want
> to unnecessarily double the amount of required memory.

Hoi, grpstats isn't designed to compute functions that work across
multiple variables. It's certainly possible to write a loop and compute
what you want by hand.

But it is possible to trick grpstats, if you are willing to add a
"dummy" index variable, and use a function such as

    corFun = @(ind) corr(data.x(ind),data.y(ind))

in a call to grpstats like

    gerpstats(data,groups,corFun,'DataVars','dummyIndex')

I can't say if this will be practical with a very large array or not.
Hope it helps.

Subject: grpstats(): how to pass multiple variables to function handle?

From: Hoi Wong

Date: 31 Aug, 2012 00:27:07

Message: 5 of 5

Sorry for the delay, I was swamped by other projects and didn't get to work on that for a while.

Thanks for your help. It's a smart idea. I tried it and it works. I had to remember to make an all 'true' logical dummy variable instead of ones because it might be misinterpreted as linear indices.

Peter Perkins <Peter.Remove.Perkins.This@mathworks.com> wrote in message <jb3cl5$i1$1@newscl01ah.mathworks.com>...
> On 11/28/2011 9:05 PM, Hoi Wong wrote:
> > I'm looking to reduce two variables (X, Y) of a grouped dataset object
> > into one variable by a function like corr(X,Y), but I cannot find a good
> > way to enter that as a function handle in grpstats() as they only accept
> > one input.
> > Is there a neat way to achieve this without concatenating two variables
> > into one matrix? My dataset is pretty large and dense, so I don't want
> > to unnecessarily double the amount of required memory.
>
> Hoi, grpstats isn't designed to compute functions that work across
> multiple variables. It's certainly possible to write a loop and compute
> what you want by hand.
>
> But it is possible to trick grpstats, if you are willing to add a
> "dummy" index variable, and use a function such as
>
> corFun = @(ind) corr(data.x(ind),data.y(ind))
>
> in a call to grpstats like
>
> gerpstats(data,groups,corFun,'DataVars','dummyIndex')
>
> I can't say if this will be practical with a very large array or not.
> Hope it helps.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
stats Hoi Wong 28 Nov, 2011 21:09:09
statistics toolbox Hoi Wong 28 Nov, 2011 21:09:09
grpstats Hoi Wong 28 Nov, 2011 21:09:09
rssFeed for this Thread

Contact us