I have an N x 3 matrix and I would like to find the mean of values in
column 3 grouped by unique rows formed from the values in columns 1 and 2.
For example the following N x 3 matrix :
1 2 3
1 2 4
2 3 5
2 4 6
2 4 8
would give the following output :
1 2 3.5
2 3 5
2 4 7
Any suggestions would be greatly appreciated.
Thanks,
Curt
John D'Errico - 14 Jul 2008 20:13 GMT
Curt <97wideglide@gmail.com> wrote in message
<g5g7kj$1i8$1@registered.motzarella.org>...
> I have an N x 3 matrix and I would like to find the mean of values in
> column 3 grouped by unique rows formed from the values in columns 1 and 2.
[quoted text clipped - 14 lines]
>
> Any suggestions would be greatly appreciated.
Find consolidator on the FEX.
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?
objectId=8354&objectType=file
HTH,
John
Roger Stafford - 14 Jul 2008 20:49 GMT
Curt <97wideglide@gmail.com> wrote in message <g5g7kj$1i8
$1@registered.motzarella.org>...
> I have an N x 3 matrix and I would like to find the mean of values in
> column 3 grouped by unique rows formed from the values in columns 1 and 2.
[quoted text clipped - 17 lines]
> Thanks,
> Curt
If you are a do-it-yourselfer, I can conceive of a method which uses the
'unique(-,rows)' function applied to the first two rows and uses the third
returned index vector along with 'accumarray' to generate your sums (and
means.) Is that enough of a hint?
(I have the uncomfortable feeling this is very similar to what John's
consolidator does. If so, my apologies to John.)
Roger Stafford
John D'Errico - 14 Jul 2008 21:49 GMT
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <g5gajd$fh5$1@fred.mathworks.com>...
> Curt <97wideglide@gmail.com> wrote in message <g5g7kj$1i8
> $1@registered.motzarella.org>...
[quoted text clipped - 30 lines]
>
> Roger Stafford
Consolidator does use unique(...,'rows') to do its
work, with a trick. When the numbers are floating
point, so a tolerance is applied, it scales the rows
and then rounds them. This way unique will do its
work properly.
John
ra ti - 14 Jul 2008 21:12 GMT
Curt <97wideglide@gmail.com> wrote in message <g5g7kj$1i8
$1@registered.motzarella.org>...
> I have an N x 3 matrix and I would like to find the mean of values in
> column 3 grouped by unique rows formed from the values in columns 1 and 2.
[quoted text clipped - 17 lines]
> Thanks,
> Curt
temp =
1 2 3
1 2 4
2 3 5
2 4 6
2 4 8
>> uidx = unique(temp(:,1:2),'rows')
uidx =
1 2
2 3
2 4
meanmat = accumarray(temp(:,1:2),temp(:,3),max(temp
(:,1:2)),@mean)
meanmat =
0 3.5000 0 0
0 0 5.0000 7.0000
>> FinalMeanMat = meanmat(sub2ind(size(meanmat),uidx
(:,1),uidx(:,2)))
FinalMeanMat =
3.5000
5.0000
7.0000
Hope that helps!!!
Curt - 14 Jul 2008 21:29 GMT
> Hope that helps!!!
It certainly does. Thank you very much!
us - 14 Jul 2008 22:17 GMT
Curt:
<SNIP block-summing evergreen...
one of the many solutions
% the data
m=-[
2 4 8
1 2 3
1 2 4
2 3 5
2 4 6
];
% the engine
[mu,mx,mx]=unique(m(:,1:2),'rows');
r=accumarray(mx,m(:,3),[],@mean);
% the result
disp([mu,r])
%{
-2 -4 7
-2 -3 5
-1 -2 3.5
%}
us