필터 지우기
필터 지우기

my code become busy for euclidean distance

조회 수: 2 (최근 30일)
fatemeh
fatemeh 2014년 2월 9일
댓글: Walter Roberson 2014년 2월 9일
i write code for euclidean distance in nested for like this:
for rsize=1:tests
for k=1:ts
dis(k,1)= pdist2( testset(rsize,1:57),tmatrix(k,1:57),'euclidean');
end
end
its run time is very long 1-2 hours why? how can i correct this code?
  댓글 수: 4
Roger Stafford
Roger Stafford 2014년 2월 9일
The point I am making, Fatemeh, is that if you actually need the output of 'pdist2' for each possible combination of values of 'rsize' and 'k', it should be possible to quickly extract it in an appropriate manner from that single "maximum" run, rather than having to go through 'pdist2' calls repeatedly. Doing it that way should save a lot of time if you do it right. That "maximum" run contains all the pairs that you will need.
In all this discussion I am assuming that the matrices 'testset' and 'tmatrix' remain unchanged for each computation of 'pdist2', except that different portions of each are used. Is that correct? Otherwise the code you have shown wouldn't make sense.
fatemeh
fatemeh 2014년 2월 9일
yes the matrices 'testset' and 'tmatrix' remain unchanged for each computation of pdist2 but what you mean about maximum run?

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

답변 (1개)

Jan
Jan 2014년 2월 9일
Did you pre-allocate the result?
dis = zeros(ts, 1)
By the way, you overwrite the result dis in each iteration over rsize.
pdist2 is vectorized, so try to omit the loops:
dis = pdist2(testset(:, 1:57), tmatrix(:,1:57), 'euclidean');
  댓글 수: 2
fatemeh
fatemeh 2014년 2월 9일
but i will need kth row of dis matrix at future
Walter Roberson
Walter Roberson 2014년 2월 9일
dis(k,:)

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

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by