Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: averaging
Date: Wed, 23 Sep 2009 06:17:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 40
Message-ID: <h9cegv$lip$1@fred.mathworks.com>
References: <h9be7r$gh5$1@fred.mathworks.com> <h9brsm$r8s$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1253686623 22105 172.30.248.35 (23 Sep 2009 06:17:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 23 Sep 2009 06:17:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1181469
Xref: news.mathworks.com comp.soft-sys.matlab:572250


"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?