# Creating a matrix of marginal distributions from a matrix of joint distributions

7 views (last 30 days)
alpedhuez on 2 Jun 2018
Answered: Prajit T R on 12 Jun 2018
I would like to create a matrix of marginal distributions from a matrix of joint distributions.
As a specific example, suppose
A=[0 0 a b; 0.1 0 c d; 0.1 0.1 e f; 0.2 0 g h; 0.2 0.1 i j;0.2 0.2 k l]
I want to create the following matrix B such that
- the first column of B collects unique elements of the entries in the first column and the second column of A (assume that entries in the first column of A are the same with entries in the second column of A). So the first column of B is
B(:,1)=[0 0.1 0.2]
- the second column of B: for each entry of the first column of B, find rows of A where the first entry of A is equal to that entry of B, and then take averages of the third entries of A (marginal distribution) Thus it will be
B(:,2)=[a; (c+e)/2; (g+i+k)/3]
- the third column of B: for each entry of the second column of B, find row of A where the second entry of A is equal to that entry of B, and then take average of the fourth entries of A. Thus it will be
B(:,3)=[(b+d+h)/3; (f+j)/2; l]

#### 1 Comment

Kaninika Pant on 5 Jun 2018
A is your joint distribution matrix? If that is the case then the rows must represent one random variable (lets say X) and the columns must represent the other random variable ( let Y). Then one element (eg (3,4) ) should have the probability of X=3 , Y=4. Then the marginal distribution of X is simply a column matrix where each row represents the probability of X=row_value with no conditions on Y. The first row will be the sum of all values in the first row of A...and so on. But what are you trying to do? What do the first and second columns of A represent? And why average?

Prajit T R on 12 Jun 2018
Hi
a=1;b=2;c=3;d=4;e=5;f=6;g=7;h=8;i=9;j=10;k=11;l=12;
A=[0 0 a b; 0.1 0 c d; 0.1 0.1 e f; 0.2 0 g h; 0.2 0.1 i j;0.2 0.2 k l];
T=A';
B_1=unique(T(1,:));
B_2=zeros(1,length(B_1));
B_3=zeros(1,length(B_1));
for i=1:length(B_1)
idx=find(T(1,:)==B_1(i));
temp=T(3,:);
B_2(i)=sum(temp(idx))/length(idx);
end
for i=1:length(B_1)
idx=find(T(2,:)==B_1(i));
temp=T(4,:);
B_3(i)=sum(temp(idx))/length(idx);
end
B=[B_1;B_2;B_3];
B=B'
Just change the values of a,b,c...l as per your requirement.
Hope this helps.
Prajit