Euclidian distance matrix
04-16-2016, 10:20 AM (This post was last modified: 04-16-2016 11:18 AM by in06khattab.)
Post: #1
 in06khattab Junior Member Posts: 9 Joined: Jan 2016
Euclidian distance matrix
Is it possible for there to be a function (in 50g or prime) to calculate the euclidean distance or the euclidean cumulative distance matrix?

e.g. From: To 04-16-2016, 01:31 PM
Post: #2
 Didier Lachieze Senior Member Posts: 1,236 Joined: Dec 2013
RE: Euclidian distance matrix
On the Prime, for the euclidean distance matrix :

Let say that you use M1 and M2 to store your sets of points
Code:
M1:=[[1.1,3.1],[2.1,4.1],[3.4,3.2]] M2:=[[0.7,2.1],[1.6,3.6],[3.2,1.6],[3.5,2.1],[3.1,4]]

Then you can get the distance matrix with:
Code:
MAKEMAT(√((M1(I,1)-M2(J,1))^2+(M1(I,2)-M2(J,2))^2),rowDim(M1),rowDim(M2))

With the fixed 2 number format setting the result is : You can easily create a program for this function.

For the cumulative distance matrix do you have a reference explaining how it is calculated ?
04-17-2016, 02:17 AM
Post: #3
 in06khattab Junior Member Posts: 9 Joined: Jan 2016
RE: Euclidian distance matrix
Thank you very much for your help in the distance matrix, it was really useful!

I think the cumulative distance must be done by hand, as there are a lot of changing external variables, as it is an example of dynamic programming and looking at the shortest path. So it is cumulative and adding the smallest value.
04-17-2016, 05:18 AM
Post: #4
 Didier Lachieze Senior Member Posts: 1,236 Joined: Dec 2013
RE: Euclidian distance matrix
If you know how to do by hand then you can write a program that will do it for you I think I have understood how to build the cumulative distance matrix. If I apply the following program:
Code:
EXPORT CDM(DM) BEGIN   LOCAL CM,j,k;   CM:=MAKEMAT(0,rowDim(DM),colDim(DM));   FOR j FROM 1 TO rowDim(DM) DO     FOR k FROM 1 TO colDim(DM) DO       IF (j==1) AND (k==1) THEN          CM(j,k):=DM(j,k);        END;       IF (j==1) AND (k>1) THEN         CM(j,k):=DM(j,k)+CM(j,k-1);       END;       IF (j>1) AND (k==1) THEN         CM(j,k):=DM(j,k)+CM(j-1,k);       END;       IF (j>1) AND (k>1) THEN         CM(j,k):=DM(j,k)+MIN(CM(j-1,k-1),CM(j-1,k),CM(j,k-1));        END;     END;   END;   RETURN CM; END;

to the euclidean distance matrix in your example I get the same result for the cumulative distance matrix : 04-17-2016, 09:23 AM
Post: #5
 in06khattab Junior Member Posts: 9 Joined: Jan 2016
RE: Euclidian distance matrix
Wow, thanks a lot, that was really useful as I was doing it by hand.
04-17-2016, 11:07 AM
Post: #6
 DrD Senior Member Posts: 1,132 Joined: Feb 2014
RE: Euclidian distance matrix
That's very good work, Didier! I envy your skill at the details.

You might also like to include the example data for making a "default" distance matrix in Didier's CDM program, (for future reference), as Didier initially described:

Code:
 EXPORT CDM(DM) BEGIN   LOCAL CM,j,k;   IF DM==0 then      M1:=[[1.1,3.1],[2.1,4.1],[3.4,3.2]];     M2:=[[0.7,2.1],[1.6,3.6],[3.2,1.6],[3.5,2.1],[3.1,4]];     DM:=MAKEMAT(√((M1(I,1)-M2(J,1))^2+(M1(I,2)-M2(J,2))^2),rowDim(M1),rowDim(M2));   END;      CM:=MAKEMAT(0,rowDim(DM),colDim(DM));   FOR j FROM 1 TO rowDim(DM) DO     FOR k FROM 1 TO colDim(DM) DO       IF (j==1) AND (k==1) THEN          CM(j,k):=DM(j,k);        END;       IF (j==1) AND (k>1) THEN         CM(j,k):=DM(j,k)+CM(j,k-1);       END;       IF (j>1) AND (k==1) THEN         CM(j,k):=DM(j,k)+CM(j-1,k);       END;       IF (j>1) AND (k>1) THEN         CM(j,k):=DM(j,k)+MIN(CM(j-1,k-1),CM(j-1,k),CM(j,k-1));        END;     END;   END;   RETURN CM; END; [code]
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)