How to create a probability table and derive probability results

4 次查看(过去 30 天)
I have, say, two probability variables: xval=[1 2] with probability xp=[0.5 0.5] and yval=[1 2] with probability yp=[0.75 0.25]
The probability table of these two, I can calculate using: [X,Y]=ndgrid(xp,yp); jpdf=X./Y; which gives me the joint probabilities with the values [1 2; 2 4].
I can calculate the values of the probabilities using: j=find(xp>0)'*find(yp>0);
But now I want to convert this, in as little code as possible, to an array which holds the combined probability values. i.e.: xyval=[1 2 3 4] with probability xyp=[0.375 0.5 0 0.125]
The 2nd term in xyp is constructed from 0.375+0.125, because there are two values of '2' in the probability table.
There are ways to do this which are unnecessarily long, but I'm looking for something in a few lines at most. Any idea's? Thanks, sim

采纳的回答

Jonathan
Jonathan 2011-11-8
Hi sim,
If I understand correctly, then 'jpdf=X./Y;' should use element-wise multiplication like 'jpdf=X.*Y;'. Also, calculating 'j' with 'j=find(xp>0)'*find(yp>0);' will only work if 'xval' and 'yval' have the values 1 through n, for xval, and 1 through m, for yval. Using 'j=xval(xp>0)'*yval(yp>0)' is a more general approach.
The result you desire can be computed using the following code.
xyval = unique(j(:));
[~, index] = ismember(j(:), xyval);
xyp = accumarray(index, jpdf(:));
I hope this helps.
~Jonathan

更多回答(1 个)

simcc
simcc 2011-11-9
This does the trick, thanks.
I was looking in the same direction, but was using accumarry incorrectly.

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by