Hello,
I was hoping that perhaps somebody could help me out with a problem that I have. It is a relatively simple operation (a weight average), just difficult to code.
Say I import 2 3x3 matrices into a cell array using the code below.
C = cell(2,1);
for ii = 1:2
C{ii} = importdata(['matrix' num2str(ii) '.txt']) ;
end
These matrices are transitional probability matrices.
I would like to calculate a weighted 'average' matrix of these 2 matrices. Unfortunately I cannot simply just calculate the average, its needs to be a weighted average.
Say for example the 2 matrices are:
0.0 0.5 0.5 2
A = 0 0 1 4
0 0.6 0.4 7
0.2 0.0 0.8 5
B = 0 0 0 0
0.3 0.0 0.7 3
The 4th column contains the number of counts for that particular row.
The weighted average formula would be
P(x) = P_A(x) x [1-B_N/(A_N+B_N)] + P_B(x) x [1-A_N/(A_N+B_N)]
Where,
P_A(x) represents the probability value in the same row at cell x for matrix A.
P_B(x) is the same thing for matrix B.
A_N is the total number of counts for the same row we are dealing with in matrix A
B_N is the total number of counts for the same row we are dealing with in matrix B
Using the numbers, the first cell would be:
= 0 x [1-5/(2+5)] + 0.2 x [1-2/(2+5)] = 0.14
So the full weighted average matrix would be
0.14 0.14 0.71
C= 0 0 1
0.1 0.4 0.5
I would really appreciate any help to code this as I'm fairly inexperienced with matlab. In practice I have about 100 matrices but I just presented 2 as an example.
Sincere Thanks
John

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 2월 24일
A =[ 0 0.5 0.5 2
0 0 1 4
0 0.6 0.4 7];
B =[ 0.2 0 0.8 5
0 0 0 0
0.3 0 0.7 3];
C1 = cat(3,A,B);
p1 = bsxfun(@rdivide,C1(:,end,:),sum(C1(:,end,:),3));
C = sum(bsxfun(@times,C1(:,1:end-1,:),p1),3)

댓글 수: 7

John
John 2012년 2월 24일
Hello Andrei,
Thank very much for your help.
Could I ask how to do one further application?
Say if I had 100 matrices instead of 2 and I imported them using this code
C = cell(100,1);
for ii = 1:100
C{ii} = importdata(['matrix' num2str(ii) '.txt']) ;
end
How would I get the weighted average of the 100 matrices?
Many thanks
John
try this is code
C1 = zeros(3,4,100); % in our case
for ii = 1:100
C1(:,:,ii) = importdata(['matrix' num2str(ii) '.txt']) ;
end
Or
C2 = cell(100,1);
for ii = 1:100
C2{ii} = importdata(['matrix' num2str(ii) '.txt']) ;
end
C1 = cat(3,C2{:});
John
John 2012년 2월 24일
Thank you very much, they both give the correct answer!
John
John 2012년 2월 28일
Hello Andrei,
Thank you for your help with my weighted average problem.
I'm just wondering if the code for pl and C needs to be changed if I change the size of the matrices say from 3x3 to 9x9?
It seems to be working fine, but I thought I should double check with you.
Many thanks
John
No, do not change
John
John 2012년 2월 28일
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2012년 2월 24일

편집:

2013년 10월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by