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:
finding the same values for a column and taking the average of corresponding values

Subject: finding the same values for a column and taking the average of corresponding values

From: Moon Datta

Date: 21 Aug, 2012 08:13:08

Message: 1 of 3

I have an array like
     1 4
     1 2
     1 6
     2 8
     2 7
     2 9

it want the output to be

     1 4
     2 8

that means when the contents of first column is same the it will take the average of the second column and so on.
please let me know any ideas for it.

Thanks in advance.

Subject: finding the same values for a column and taking the average of

From: Lothar Schmidt

Date: 21 Aug, 2012 08:36:06

Message: 2 of 3

Am 21.08.2012 10:13, schrieb Moon Datta:
> I have an array like
> 1 4
> 1 2
> 1 6
> 2 8
> 2 7
> 2 9
>
> it want the output to be
>
> 1 4
> 2 8
> that means when the contents of first column is same the it will take
> the average of the second column and so on.
> please let me know any ideas for it.
>
> Thanks in advance.

test =

      1 4
      1 2
      1 6
      2 8
      2 7
      2 9

k=unique(test(:,1))

k =

      1
      2

for z=1:numel(k);
result(z,1)=k(z);
result(z,2)=mean(test(find(test(:,1)==k(z)),2));
end

result =

      1 4
      2 8

Subject: finding the same values for a column and taking the average of corresponding values

From: Bruno Luong

Date: 21 Aug, 2012 08:54:07

Message: 3 of 3

a=[ 1 4
    1 2
    1 6
    2 8
    2 7
    2 9]

[u, I, J] = unique(a(:,1),'stable');
[u accumarray(J(:),a(:,2),[],@mean)]

% Bruno

Tags for this Thread

No tags are associated with 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