Hello. I have an issue with a code performing some matrix operations. It is getting to slow, because I am using loops. I am trying for some time to optimize this code and to re-write it with less or without loops. Until now unsuccessful. Can you please help me solve this:
pb = rand(10,15);
data = rand(10000,15);
[rP, cP] = size(pb);
[r, c] = size(data);
dist=zeros(r, rP);
C = zeros(r,1);
for h=1:r
min=inf;
for w=1:rP
dist(h,w)= sum((data(h,:) - pb(w,:)).^2);
if min > dist(h,w)
min= dist(h,w);
near_clust=w;
end
end
C(h) = near_clust ;
end
For large dimension the execution time of these two loops is very high. How can I optimize this?
Thank you,

 채택된 답변

Joseph Cheng
Joseph Cheng 2014년 9월 12일
편집: Joseph Cheng 2014년 9월 12일

0 개 추천

you can get rid of the second for loop;
tic
for h=1:r
dist1(h,:)= sum((repmat(data(h,:),rP,1) - pb).^2,2)';
[mindist near_clust] = min(dist1(h,:));
C1(h) = near_clust;
end
toc
here i'm performing all of what you're doing in the for loop and looking for the minimum. as the second loop does distance calculations of one point vs all pb points and replace min value. here using repmat i can perform a matrix subtraction and find all the distances then use min() to find the minimum distance.
there is a question of what if two clusters have the same min distance?

추가 답변 (1개)

Guillaume
Guillaume 2014년 9월 12일

0 개 추천

First of all, don't use min as the name of a variable as you can't then matlab's min function which you actually want.
Secondly, using matlab's min function you can calculate C in one go, outside both for loop:
[~, C] = min(dist, [], 2); %won't work if you have a variable called min
Finally, you can eliminate the inner loop entirely:
for h=1:r
dist(h, :) = sum(bsxfun(@minus, data(h,:), pb).^2, 2)';
end
[~, C] = min(dist, [], 2);

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 9월 12일

댓글:

2014년 9월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by