0

I have two matrices ((intx(i),inty(i),intz(i)) and Ceres(j)) with coordinates, and I need to find the two points closest to each other. From the MATLAB code I would expect to get the distance between the point in the first matrix, that is closest to a point on the second matrix, Ceres.

However, instead of getting this minimum value as a single number in disttemp I get an array of length(ceres_lim).

What am I doing wrong?

disttemp = 100000; % large number greater that the expected min distance

for i = 1:length(intz) % size of interpolated line point vector

    for j = ceres_lim % array of indices close to line 

        distcur = pdist2([intx(i),inty(i),intz(i)],[Ceres(j,1),Ceres(j,2),Ceres(j,3)],'euclidean'); % distance between point on interpolated line (i) and scatter point on Ceres (j)

        if distcur < disttemp 
            disttemp = distcur; % statement saves smallest value of distcur to disttemp to find min distance
        end
    end
end
Julie
  • 1
  • 3
    I can't reproduce your error. What are the dimensions of your arrays? Also, `pdist2` is designed to work on arrays of points, so you should be able to do `distall = pdist2([intx, inty, intz], Ceres)` and get a `length(intz) x ceres_lim` matrix of all of the distances at once without the loops. – beaker Jul 16 '19 at 14:46
  • (intx,inty,intz) is of length 2000, and Ceres(x,y,z) is of length 98306, however there is only looped over 9400 values for Ceres. I hence have 2000 x 9400 = 18 800 000 distances to go through, which is why I've made a loop, so that the program only processes one at a time. – Julie Jul 17 '19 at 09:57
  • Okay, the dimensions of your arrays look good, so I have no idea why you're getting an array instead of a single value. First, I'd make sure that all variables are cleared from the workspace to ensure that old values aren't messing you up. Second, I'd try to reproduce the error by creating smaller arrays of random values like `intx = rand(1, 10); Ceres = rand(1,13);...` and so on. If you can, then it would be helpful if you could post the full example with the code you used to generate the random data so we can see exactly what you see. – beaker Jul 17 '19 at 18:32
  • I see your point about reducing the memory requirements, but I'd still do away with the `j` loop and use `min` instead like: `distcur = pdist2([intx(i),inty(i),intz(i)],Ceres(ceres_lim, :),'euclidean'); disttemp = min([disttemp, distcur]);` – beaker Jul 17 '19 at 18:36

0 Answers0