matlabplotmatlab-figurepdist

Plot distances between points matlab


I've made a plot of 10 points

10 10 248,628959661970 66,9462583977501 451,638770451973 939,398361884535 227,712826026548 18,1775336366957 804,449583613070 683,838613746355 986,104241895970 783,736480083219 29,9919502693899 534,137567882728 535,664190667238 885,359450931142 87,0772199008924 899,004898906140 990 990

With the first column as x-coordinates and the other column as y-coordinates

Leading to the following Plot: Using the following code: scatter(Problem.Points(:,1),Problem.Points(:,2),'.b') Plot

I then also calculated the euclidean distances using Problem.DistanceMatrix = pdist(Problem.Points); Problem.DistanceMatrix = squareform(Problem.DistanceMatrix);

I replaced the distances by 1*10^6 when they are larger than a certain value. This lead to the following table:

Euclidean distances

Then, I would like to plot the lines between the corresponding points, preferably with their distances, but only in case the distance < 1*10^6.

Specifically i want to plot the line [1,2] [1,4] [1,7] [2,4] etc.

My question is, can this be done and how?


Solution

  • Assuming one set of your data is in something called xdata and the other in ydata and then the distances in distances, the following code should accomplish what you want.

    hold on
    for k = 1:length(xdata)
        for j = 1:length(ydata)
            if(distances(k,j) < 1e6)
                plot([xdata(k) xdata(j)], [ydata(k) ydata(j)]);
            end
        end
    end
    

    You just need to iterate through your matrix and then if the value is less than 1e6, then plot the line between the kth and jth index points. This will however double plot lines, so it will plot from k to j, and also from j to k, but it is quick to code and easy to understand. I got the following plot with this.

    enter image description here