Product of probabilities by group
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (2)
the cyclist
el 8 de Dic. de 2012
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
el 8 de Dic. de 2012
0 votos
Categorías
Más información sobre Random Number Generation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!