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:
mean of unique values

Subject: mean of unique values

From: enviro

Date: 31 May, 2010 15:24:31

Message: 1 of 4

How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea?

input=[
     2 10 50
     1 20 100
     2 30 150
     1 40 200
     1 60 300];

output=[
     1 40 100
     2 20 200
      ];

Subject: mean of unique values

From: us

Date: 31 May, 2010 15:40:05

Message: 2 of 4

enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <2037152987.255198.1275319502015.JavaMail.root@gallium.mathforum.org>...
> How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea?
>
> input=[
> 2 10 50
> 1 20 100
> 2 30 150
> 1 40 200
> 1 60 300];
>
> output=[
> 1 40 100
> 2 20 200
> ];

a hint:
- look at jd's FEX submission

http://www.mathworks.com/matlabcentral/fileexchange/8354-consolidator

us

Subject: mean of unique values

From: Roger Stafford

Date: 31 May, 2010 21:13:02

Message: 3 of 4

enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <2037152987.255198.1275319502015.JavaMail.root@gallium.mathforum.org>...
> How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea?
>
> input=[
> 2 10 50
> 1 20 100
> 2 30 150
> 1 40 200
> 1 60 300];
>
> output=[
> 1 40 100
> 2 20 200
> ];
- - - - - - - - -
  Call your input matrix A and the desired output B. Then do this:

 B = sortrows(A,1); % Sort according to col. 1
 p = find([true;diff(B(:,1))~=0;true]); % Point to steps in col. 1
 n = size(A,2);
 C = cumsum([zeros(1,n-1);B(:,2:n)]); % cumsums in other cols.
 B = [B(p(1:end-1),1),diff(C(p,:))./repmat(diff(p),1,n-1)]; % Get means

  Comments: In the last line, diff(C(p,:)) gets the needed sums by taking differences of the appropriate cumulative sum values. Dividing by diff(p) counts gives the corresponding mean values.

  Note: Presumably you meant the column 3 values of your output to be the reversal of what you wrote.

Roger Stafford

Subject: mean of unique values

From: Steve Amphlett

Date: 31 May, 2010 22:59:05

Message: 4 of 4

enviro <farhadnejadkoorki@yahoo.co.uk> wrote in message <2037152987.255198.1275319502015.JavaMail.root@gallium.mathforum.org>...
> How to mean columns(2 to end )of a matrix based on the unique values of column 1. Any idea?
>
> input=[
> 2 10 50
> 1 20 100
> 2 30 150
> 1 40 200
> 1 60 300];
>
> output=[
> 1 40 100
> 2 20 200
> ];

Does column 1 always contain positive integers?

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