Improve performance of code to solve equation

조회 수: 2 (최근 30일)
Sitherion
Sitherion 2016년 12월 8일
댓글: Walter Roberson 2016년 12월 8일
I am quite novice to MATLAB and I am struggling to find a solution to this equation.
Where the matrices' dimensions are {λ} = N×K, {Y} = N×D, {π} = 1×K, and {μ} = D×K.
What I have created looks more like a monstrosity than an efficient MATLAB code, and that is due to a serious lack of MATLAB skills.
Actually, I decided to solve this step by step, in order to get myself familiarised with how MATLAB works. I ended up with an ultra inefficient code that I cannot even combine it. Even the final result is wrong, as I want to create a NxK matrix. Any guidance would be much appreciated.
%Dimensions:
nn = 10;
dd = 7;
kk = 5;
%Initial variables:
lambda0 = rand(nn,kk);
sigma = rand(1);
Y = rand(nn,dd);
mu = rand(dd,kk);
%Calculate pies:
First_part = log(pie(:)./(1.-pie(:))); % Kx1 vector
%Calculate Second part:
for n = 1:nn
for d = 1:dd
lambda_mu(d,:) = lambda0(n,:).*mu(d,:); %lambdamu is a DxK matrix
end
lambda_mu2(n,:) = sum(lambda_mu,2); %This is a NXD matrix
end
%Y-lambdamu2:
for n = 1:nn
YY(n,:) = Y(n,:)-lambda_mu2(n,:); % This is a NxD vector
end
%YY*mu:
Second = (YY*mu)./sigma^2; % NxK
%Third:
Third = (mu'*mu)./2*sigma^2; % KxK
%Final:
for n=1:nn
Final_part = transpose(First_part(:))+Second(n,:)-Third;
end

답변 (1개)

Walter Roberson
Walter Roberson 2016년 12월 8일
Your notation is not consistent with the possibility of y, mu, and lambda all being 2D matrices with subscript indicating columns and superscript (n) indicating row. Your sigma would have to be summing columns in that case, but the sum would have to be subtracted from the row y superscript (n).
The intended meaning of your superscripts and subscripts in the equation are not clear.
  댓글 수: 2
Sitherion
Sitherion 2016년 12월 8일
Thanks for the answer. The notation is correct though: http://mlg.eng.cam.ac.uk/zoubin/course04/lect7var.pdf (page 9 and 10)
Walter Roberson
Walter Roberson 2016년 12월 8일
Top of page 10:
Assume a data set D = {y(1), ..., y(N)} of N points.
Notice that each of the elements are points, so D is a vector, not a 2D array.

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

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by