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:
taking average of different parts of a vector

Subject: taking average of different parts of a vector

From: Sean Douglas

Date: 21 Feb, 2011 16:32:04

Message: 1 of 3

Hey,
I am looking for a simple way to take an average of certain cells.
I have 2 vector in the format below:
10 3
10 6
10 2
12 3
12 7
12 9
Is there a way I can take the average of the second column for when the first column has the same value?
In the case here I want: 10 to correspond to mean(3 6 2)
And 12 to correspond to mean(3 7 9)

Subject: taking average of different parts of a vector

From: Sean de

Date: 21 Feb, 2011 16:49:04

Message: 2 of 3

"Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <iju424$h5a$1@fred.mathworks.com>...
> Hey,
> I am looking for a simple way to take an average of certain cells.
> I have 2 vector in the format below:
> 10 3
> 10 6
> 10 2
> 12 3
> 12 7
> 12 9
> Is there a way I can take the average of the second column for when the first column has the same value?
> In the case here I want: 10 to correspond to mean(3 6 2)
> And 12 to correspond to mean(3 7 9)

A = [10 3
10 6
10 2
12 3
12 7
12 9];
[Amean] = accumarray(A(:,1),A(:,2),[],@(x)mean(x));
Amean(unique(A(:,1)))

Subject: taking average of different parts of a vector

From: Paulo Silva

Date: 21 Feb, 2011 16:54:07

Message: 3 of 3

"Sean Douglas" <seanjdouglas@hotmail.com> wrote in message <iju424$h5a$1@fred.mathworks.com>...
> Hey,
> I am looking for a simple way to take an average of certain cells.
> I have 2 vector in the format below:
> 10 3
> 10 6
> 10 2
> 12 3
> 12 7
> 12 9
> Is there a way I can take the average of the second column for when the first column has the same value?
> In the case here I want: 10 to correspond to mean(3 6 2)
> And 12 to correspond to mean(3 7 9)

a=[10 3
10 6
10 2
12 3
12 7
12 9]
u=unique(a(:,1));
for f=1:numel(u)
[r,c]=find(a==u(f));
fprintf('Found the number %d, %d times and the average is %d\n',u(f),numel(r),mean(a(r,2)))
end

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