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:
Need to Sort Data

Subject: Need to Sort Data

From: Joe Tomaino

Date: 1 Oct, 2010 23:51:05

Message: 1 of 5

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

Subject: Need to Sort Data

From: Ross W

Date: 2 Oct, 2010 01:10:21

Message: 2 of 5

"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

use accumarray with mean as the function

Ross

Subject: Need to Sort Data

From: Roger Stafford

Date: 2 Oct, 2010 01:44:05

Message: 3 of 5

"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

Subject: Need to Sort Data

From: Roger Stafford

Date: 2 Oct, 2010 05:49:05

Message: 4 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i862p4$omf$1@fred.mathworks.com>...
> .......
> [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)];
> .......
- - - - - - - -
  That 'unique' call is not really necessary.

 [u,p] = sort(X);
 c = [0;cumsum(Y(p))];
 f = find([true;diff(u)~=0;true]);
 Z = [u(f(1:end-1)),diff(c(f))./diff(f)];

  Note: For very long vectors, this 'cumsum' method can develop larger round off errors than methods using accumarray.

Roger Stafford

Subject: Need to Sort Data

From: Joe Tomaino

Date: 6 Oct, 2010 00:51:24

Message: 5 of 5

Thanks to both of you, I finally got it all figured out.

-Joe

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i86h4h$666$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i862p4$omf$1@fred.mathworks.com>...
> > .......
> > [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)];
> > .......
> - - - - - - - -
> That 'unique' call is not really necessary.
>
> [u,p] = sort(X);
> c = [0;cumsum(Y(p))];
> f = find([true;diff(u)~=0;true]);
> Z = [u(f(1:end-1)),diff(c(f))./diff(f)];
>
> Note: For very long vectors, this 'cumsum' method can develop larger round off errors than methods using accumarray.
>
> 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