I guess, the problem occurs, when several matching values are found by:
t=find(dist_from_current_point==nearest_dist);
Maybe it satisfies your need to choose 1 only:
for j = 1:size(matrix_num, 1)
if isnan(matrix_num(j, 1))
dist_from_current_point = (matrix_num(j, 2) - matrix_num(:, 2)) .^ 2;
dist_from_current_point(j) = Inf;
[nearest_dist, t] = min(dist_from_current_point);
matrix_num(j,1)=matrix_num(t,1);
end
end
If matrix_num is large and has a few NaN values only, this is more efficient:
index = find(isnan(matrix_num(:, 1));
for k = index(:).'
dist = abs(matrix_num(j, 2) - matrix_num(:, 2));
[~, ind] = min(dist);
matrix_num(k,1) = matrix_num(ind, 1);
end
Best Answer