Improve performance of code to solve equation

1 次查看(过去 30 天)
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 个评论
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 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by