7 views (last 30 days)

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]

Please advise the steps. Thank you.

Prajit T R
on 12 Jun 2018

Hi

The following code will help you perform the steps you need:

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

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/403714-creating-a-matrix-of-marginal-distributions-from-a-matrix-of-joint-distributions#comment_575237

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/403714-creating-a-matrix-of-marginal-distributions-from-a-matrix-of-joint-distributions#comment_575237

Sign in to comment.