Reputation: 1
I have this MATLAB code as part of my project, as per the information I got from the professor's report, most of the time is spent in the while
loop. Any suggestions regarding how can
I improve the efficiency? Generally, how can I make the for loops more efficient?
% p is 2D matrix of big size
s=size(p);
pp=p;
p=zeros(s(1),s(2));
while(norm(p-pp)>0.05 )
p=pp;
for n=1:N
z=0;
for miu=1:C
z = z + p(n,miu) * funQ(n,miu,p,R,N,C); % call function funQ
end
for lambda=1:C
pp(n,lambda) = (p(n,lambda) * funQ(n,lambda,p,R,N,C))/z; % call function funQ
end
end
end
Upvotes: 0
Views: 50
Reputation: 7186
I don't know what funQ
does but if it is amenable to vectorization then, you can try the following:
Replace
for miu=1:C
z = z + p(n,miu) * funQ(n,miu,p,R,N,C); % call function funQ
end
With
miu=1:C
z = sum(p(n,miu) * funQ(n,miu,p,R,N,C)); % call function funQ
Similarly,
Replace
for lambda=1:C
pp(n,lambda) = (p(n,lambda) * funQ(n,lambda,p,R,N,C))/z; % call function funQ
end
With
pp(n,:) = (p(n,lambda) * funQ(n,lambda,p,R,N,C))/z; % call function funQ
Upvotes: 0