From: Ryan on
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?
From: mat001 on
"Ryan " <jessupr(a)tcd.ie> wrote in message <ht3cis$r6u$1(a)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.
From: Sean on
"Ryan " <jessupr(a)tcd.ie> wrote in message <ht3cis$r6u$1(a)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);
From: Ryan on
"Sean " <sean.dewolski(a)nospamplease.umit.maine.edu> wrote in message <ht3e7c$gjv$1(a)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.
From: Jos (10584) on
"Ryan " <jessupr(a)tcd.ie> wrote in message <ht5qse$t4v$1(a)fred.mathworks.com>...
> "Sean " <sean.dewolski(a)nospamplease.umit.maine.edu> wrote in message <ht3e7c$gjv$1(a)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