How to create a probability table and derive probability results

조회 수: 5 (최근 30일)
simcc
simcc 2011년 11월 8일
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