Product of probabilities by group

Hi,
I have matrix that contains in the first column a group identifier and in the second column a probability. I want to generate, for each group identifier, the product of the probabilities that correspond to that group.
For example
X=[1 0.5;1 0.3; 1 0.4; 2 0.7; 2 0.4];
What I want is to generate a matrix
Y=[1 (0.5*0.3*0.4);2 (0.7*0.4)];
and I can't do it "by hand" because the number of groups and the number of probabilities is huge.
Any ideas? Thanks.

 채택된 답변

Matt Fig
Matt Fig 2012년 12월 8일
편집: Matt Fig 2012년 12월 8일

1 개 추천

Here is another method. This might work best if you have groups in increments of one because it uses the group number as the index of the return array.
Y = accumarray(X(:,1),X(:,2),[],@prod);
Y(I) is the product of the probabilities belonging to the Ith group. To understand this, look at:
X = [1 0.5000
1 0.3000
1 0.4000
2 0.7000
4 0.5000
4 0.6000
6 0.7000
6 0.2000]; % No group 3 or 5.
Y = accumarray(X(:,1),X(:,2),[],@prod);
Since there is no group 3 or 5 in X, Y(3) and Y(5) are zero. If there are missing groups in your array, you can still get to the desired form like:
Z = [find(Y),Y(Y~=0)];

추가 답변 (2개)

the cyclist
the cyclist 2012년 12월 8일

1 개 추천

Here's one way:
[uniqueX1,~,index] = unique(X(:,1));
numberUniqueX1 = numel(uniqueX1);
Y = nan(numberUniqueX1,1);
for nx = 1:numberUniqueX1
Y(nx) = prod(X(index==nx,2));
end
Fernando
Fernando 2012년 12월 8일

0 개 추천

Thanks for your help!

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2012년 12월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by