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:
get a mean of indexed data without using for loop

Subject: get a mean of indexed data without using for loop

From: Ryan

Date: 20 May, 2010 13:14:04

Message: 1 of 5

I'm trying to obtain a mean of numbers, using different indices, without using a 'for' loop. For example, say I have the following matrix x:
x = [101 3; ...
101 2; ...
101 1; ...
102 8; ...
103 5; ...
103 5; ...
104 6; ...
104 8; ..
104 5.2; ...
104 7; ...
109 5; ...
109 7];

I would like to get the mean in column 2 for all instances of column 1==101, and the mean in column 2 for all instances of column 1==101, etc. Does anyone know of an efficient manner for doing this without using a 'for' loop?

Subject: get a mean of indexed data without using for loop

From: mat001

Date: 20 May, 2010 13:38:04

Message: 2 of 5

"Ryan " <jessupr@tcd.ie> wrote in message <ht3cis$r6u$1@fred.mathworks.com>...
> I'm trying to obtain a mean of numbers, using different indices, without using a 'for' loop. For example, say I have the following matrix x:
> x = [101 3; ...
> 101 2; ...
> 101 1; ...
> 102 8; ...
> 103 5; ...
> 103 5; ...
> 104 6; ...
> 104 8; ..
> 104 5.2; ...
> 104 7; ...
> 109 5; ...
> 109 7];
>
> I would like to get the mean in column 2 for all instances of column 1==101, and the mean in column 2 for all instances of column 1==101, etc. Does anyone know of an efficient manner for doing this without using a 'for' loop?

Try

 mean(x(:,:)) %may be this is what you asked for.

Subject: get a mean of indexed data without using for loop

From: Sean

Date: 20 May, 2010 13:42:04

Message: 3 of 5

"Ryan " <jessupr@tcd.ie> wrote in message <ht3cis$r6u$1@fred.mathworks.com>...
> I'm trying to obtain a mean of numbers, using different indices, without using a 'for' loop. For example, say I have the following matrix x:
> x = [101 3; ...
> 101 2; ...
> 101 1; ...
> 102 8; ...
> 103 5; ...
> 103 5; ...
> 104 6; ...
> 104 8; ..
> 104 5.2; ...
> 104 7; ...
> 109 5; ...
> 109 7];
>
> I would like to get the mean in column 2 for all instances of column 1==101, and the mean in column 2 for all instances of column 1==101, etc. Does anyone know of an efficient manner for doing this without using a 'for' loop?


One way:

x = [101 3; ...
101 2; ...
101 1; ...
102 8; ...
103 5; ...
103 5; ...
104 6; ...
104 8; ...
104 5.2; ...
104 7; ...
109 5; ...
109 7];

ndxs = unique(x(:,1));
bins = histc(x(:,1),ndxs);
C = mat2cell(x(:,2),bins);

my_means = cellfun(@mean,C);

Subject: get a mean of indexed data without using for loop

From: Ryan

Date: 21 May, 2010 11:30:22

Message: 4 of 5

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ht3e7c$gjv$1@fred.mathworks.com>...

>
>
> One way:
>
> x = [101 3; ...
> 101 2; ...
> 101 1; ...
> 102 8; ...
> 103 5; ...
> 103 5; ...
> 104 6; ...
> 104 8; ...
> 104 5.2; ...
> 104 7; ...
> 109 5; ...
> 109 7];
>
> ndxs = unique(x(:,1));
> bins = histc(x(:,1),ndxs);
> C = mat2cell(x(:,2),bins);
>
> my_means = cellfun(@mean,C);

Thanks, Sean, that works perfectly! I've had need for this many times so it will come in very handy.

Subject: get a mean of indexed data without using for loop

From: Jos (10584)

Date: 21 May, 2010 15:25:21

Message: 5 of 5

"Ryan " <jessupr@tcd.ie> wrote in message <ht5qse$t4v$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ht3e7c$gjv$1@fred.mathworks.com>...
>
> >
> >
> > One way:
> >
> > x = [101 3; ...
> > 101 2; ...
> > 101 1; ...
> > 102 8; ...
> > 103 5; ...
> > 103 5; ...
> > 104 6; ...
> > 104 8; ...
> > 104 5.2; ...
> > 104 7; ...
> > 109 5; ...
> > 109 7];
> >
> > ndxs = unique(x(:,1));
> > bins = histc(x(:,1),ndxs);
> > C = mat2cell(x(:,2),bins);
> >
> > my_means = cellfun(@mean,C);
>
> Thanks, Sean, that works perfectly! I've had need for this many times so it will come in very handy.

Here is another, faster way using ACCUMARRAY directly instead of CELLFUN:

[ix,ix,ix] = unique(x(:,1)) ; % equals [unused1, unused2,ix] = unique(x(:,1))
C = accumarray(ix,x(:,2),[],@mean)

hth
Jos

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