Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Need to Sort Data
Date: Sat, 2 Oct 2010 01:44:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 31
Message-ID: <i862p4$omf$1@fred.mathworks.com>
References: <i85s59$2ke$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1285983845 25295 172.30.248.37 (2 Oct 2010 01:44:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 2 Oct 2010 01:44:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:675192

"Joe Tomaino" <JLTomaino@aol.com> wrote in message <i85s59$2ke$1@fred.mathworks.com>...
> Hello, 
> 
> I'm trying to sort some simple data and I'm having a tough time finding a reasonably elegant solution. My data is composed of two columns, X and Y. Some of the values of X are repeated, and when that's the case, I want my program to average the corresponding values of Y.
> 
> For example, if my data were:
> 
> 1 1
> 1 2
> 2 3
> 3 4
> 
> I'd want it to return:
> 
> 1 1.5
> 2 3
> 3 4
> 
> Any help would be great. Thanks
> 
> -Joe
- - - - - - - - -
  You wrote "sort data" in your subject line, so I assume that the X column is in general unsorted.

 [u,ig,n] = unique(X);
 [t,p] = sort(n);
 c = [0;cumsum(Y(p))];
 f = find([true;diff(t)~=0;true]);
 Z = [u,diff(c(f))./diff(f)];

Roger Stafford