optimization - Speed-efficient classification for complex vectors in MATLAB -


i trying optimize piece of code , rid of nested loop implemented. finding difficulties in applying matrix pdist function

for example, 1+j // -1+j // -1+j // -1-j initial points , trying detect 0.5+0.7j point belong min distance approach .
appreciated

function result = mindisdetector( newpoints, initialpoints) result = []; i=1:length(newpoints)     mindistance = inf;     j=1:length(initialpoints)          x = [real(newpoints(i)) imag(newpoints(i));real(initialpoints(j)) imag(initialpoints(j))];         d = pdist(x,'euclidean');          if d < mindistance             mindistance = d;             index = j;         end     end     result = [result; initialpoints(index)];  end      end 

you can use efficient euclidean distance calculation listed in speed-efficient classification in matlab vectorized solution -

%// setup input vectors of real , imaginary mx2 & nx2 arrays = [real(initialpoints) imag(initialpoints)]; bt = [real(newpoints).' ; imag(newpoints).'];  %// calculate squared euclidean distances. 1 of vectorized %// variations of performing efficient euclidean distance calculation using  %// matrix multiplication linked earlier in post. dists = [a.^2 ones(size(a)) -2*a ]*[ones(size(bt)) ; bt.^2 ; bt];  %// find min index each bt & extract corresponding elements initialpoints [~,min_idx] = min(dists,[],1); result_vectorized = initialpoints(min_idx); 

quick runtime tests newpoints 400 x 1 & initialpoints 1000 x 1:

-------------------- original approach elapsed time 1.299187 seconds. -------------------- proposed approach elapsed time 0.000263 seconds. 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -