Thread Subject: averaging

Subject: averaging

From: jenya polyakova

Date: 22 Sep, 2009 21:06:03

Message: 1 of 7

I know it can be done in one line. But I am not sure how to do it. Anybody please?
I have a matrix. Say,
col1 col2
23 0.6
12 0.9
12 1.0
23 0.5
how to average col2 so that it averages by the same id number represented by col1 i.e it should give me the column of [(0.6+0.5)/2 ; (0.9+0.1)/2]. Thanks so much.

 

Subject: averaging

From: arich82

Date: 23 Sep, 2009 00:59:02

Message: 2 of 7

"jenya polyakova" <jenya56@yahoo.com> wrote in message <h9be7r$gh5$1@fred.mathworks.com>...
> I know it can be done in one line. But I am not sure how to do it. Anybody please?
> I have a matrix. Say,
> col1 col2
> 23 0.6
> 12 0.9
> 12 1.0
> 23 0.5
> how to average col2 so that it averages by the same id number represented by col1 i.e it should give me the column of [(0.6+0.5)/2 ; (0.9+1.0)/2]. Thanks so much.
>
>

Assuming col1 is exactly integer so we can use it in boolean tests:

>> mean([col2(col1==23), col2(col1==12)]) % assumes equal numbers of each id


A more robust method would use round(col1) for the boolean. If you want some truly horrendous notation that keeps everything on one line and references only one matrix:

>> A = [23 0.6 ; 12 0.9 ; 12 1.0 ; 23 0.5]
>> [mean(A(round(A(:, 1))==23, 2)), mean(A(round(A(:, 1))==12, 2))]
% mean called twice


Good luck parsing that. It would be wiser to explicitly pull out the indices instead:
>> id1 = round(23);
>> id2 = round(12);
>> col1 = round(A(:, 1));
>> col2 = A(:, 2);
>> k1 = col1 == id1;
>> k2 = col1 == id2;
>> [mean(col2(k1)), mean(col2(k2))]


Hope this helps.

--

Subject: averaging

From: jenya polyakova

Date: 23 Sep, 2009 06:17:03

Message: 3 of 7

"arich82 " <|a|r|i|c|8|2|@hotmail.com> wrote in message <h9brsm$r8s$1@fred.mathworks.com>...
> "jenya polyakova" <jenya56@yahoo.com> wrote in message <h9be7r$gh5$1@fred.mathworks.com>...
> > I know it can be done in one line. But I am not sure how to do it. Anybody please?
> > I have a matrix. Say,
> > col1 col2
> > 23 0.6
> > 12 0.9
> > 12 1.0
> > 23 0.5
> > how to average col2 so that it averages by the same id number represented by col1 i.e it should give me the column of [(0.6+0.5)/2 ; (0.9+1.0)/2]. Thanks so much.
> >
> >
>
> Assuming col1 is exactly integer so we can use it in boolean tests:
>
> >> mean([col2(col1==23), col2(col1==12)]) % assumes equal numbers of each id
>
>
> A more robust method would use round(col1) for the boolean. If you want some truly horrendous notation that keeps everything on one line and references only one matrix:
>
> >> A = [23 0.6 ; 12 0.9 ; 12 1.0 ; 23 0.5]
> >> [mean(A(round(A(:, 1))==23, 2)), mean(A(round(A(:, 1))==12, 2))]
> % mean called twice
>
>
> Good luck parsing that. It would be wiser to explicitly pull out the indices instead:
> >> id1 = round(23);
> >> id2 = round(12);
> >> col1 = round(A(:, 1));
> >> col2 = A(:, 2);
> >> k1 = col1 == id1;
> >> k2 = col1 == id2;
> >> [mean(col2(k1)), mean(col2(k2))]
>
>
> Hope this helps.
>
> --

but I do not know that id1=round(23). The matrix A was used just as example. My matrix A is HUGE. What I need is to sort by col 1 and then average col2 according to col1. Any suggestions?

Subject: averaging

From: Bruno Luong

Date: 23 Sep, 2009 06:48:01

Message: 4 of 7

Use similar technique from this thread. Call UNIQUE on the first column if needed:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117

Bruno

Subject: averaging

From: arich82

Date: 23 Sep, 2009 07:44:05

Message: 5 of 7

> but I do not know that id1=round(23). The matrix A was used just as example. My matrix A is HUGE. What I need is to sort by col 1 and then average col2 according to col1. Any suggestions?

Does this work?

[dummy, dummy, i1] = unique(A(:,1));
avg = accumarray(i1, A(:,2), [], @mean);

--

Subject: averaging

From: arich82

Date: 23 Sep, 2009 07:47:04

Message: 6 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h9cgb0$g2s$1@fred.mathworks.com>...
> Use similar technique from this thread. Call UNIQUE on the first column if needed:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117
>
> Bruno

... And had I refreshed my browser before posting, I would have seen that Bruno already solved your problem.

Sorry for the duplicating the post (but at least I learned something in the process!).

--

Subject: averaging

From: Jos

Date: 23 Sep, 2009 11:08:04

Message: 7 of 7

"arich82 " <|a|r|i|c|8|2|@hotmail.com> wrote in message <h9cjpo$sma$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h9cgb0$g2s$1@fred.mathworks.com>...
> > Use similar technique from this thread. Call UNIQUE on the first column if needed:
> >
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/261117
> >
> > Bruno
>
> ... And had I refreshed my browser before posting, I would have seen that Bruno already solved your problem.
>
> Sorry for the duplicating the post (but at least I learned something in the process!).
>
> --

Here is another "oneliner" using cells

% data
A = [23 0.6 ; 12 0.9 ; 12 1.0 ; 23 0.5] ;
% engine
R = cellfun(@mean,group2cell(A(:,2),A(:,1)))

My GROUP2CELL submission can be found here:
http://www.mathworks.com/matlabcentral/fileexchange/11192

Jos

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
group2cell Jos (10584) 23 Sep, 2009 07:09:08
average a column Sprinceana 23 Sep, 2009 02:09:14
column Sprinceana 23 Sep, 2009 02:09:14
average Sprinceana 23 Sep, 2009 02:09:07
rssFeed for this Thread

Contact us at files@mathworks.com