Code optimization for efficiency

I created code to match points from one vector to another based on the Euclidean distance and verified that it worked correctly.

However, this takes too much time. In fact, I created a matrix for the Euclidean distance A and B vectors and found its minimum. After I designate the mapping of these points, I delete the row and column from the Euclidean matrix, marking them as NaN for the next mapping.

Could this code be more efficient, because now it is very slow ...

Euclid = distance(A,B); % calculates euclid distance column v/s column wise. 
for var = 1 : value
    %# finds the min of Euclid and its position, when Euclid is viewed as a 1D array
    [~, position] = min(Euclid(:)); 

    %#transform the index in the 1D view to 2 indices
    [i,j] = ind2sub(size(Euclid),position);

    %display(strcat(num2str(i),32, num2str(j)));

    mapping = [A(1,i) A(2,i) B(1,j) B(2,j)];
    fprintf(FID,'%d %d %d %d\n', mapping );

    Euclid( i , : ) = NaN;
    Euclid( : , j ) = NaN;
    %counter = counter + 1;
end    

The problem is that for the 5000 X 5000 matrix, the code just hangs for a long time ...

Can someone help me ...

+3
source share
1 answer

, - - , , 1-D . 1-D, . , , , 1-D . Matlab, .

. , ( ) -, , (, 't NaN, ).

. for, , , (- , ). i i-1 , O (n ^ 2).

, , min , , O (n log n), .

, , . , Matlab, , , .

, A , B. B . node, , . , , .

, , , Python ++, MPI, Amazon Web Services , .

+5

All Articles