Calculating Distance Matrix in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter brydustin
  • Start date Start date
  • Tags Tags
    Mathematica Matrix
Click For Summary

Discussion Overview

The discussion revolves around calculating a distance matrix in Mathematica, specifically focusing on the Euclidean distance between row vectors in a given matrix. Participants share their attempts, code snippets, and seek solutions to issues encountered while transitioning from Matlab to Mathematica.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant shares their initial attempt to create a distance matrix using nested loops in Mathematica, expressing difficulty in getting the loop to work correctly.
  • Another participant suggests using the `Outer` function combined with a custom function to compute the squared differences between rows, providing a working example of this approach.
  • A third participant repeats the example provided, indicating that they encountered a runtime error and inquires about the version of Mathematica being used.
  • A later reply advises refreshing the page and restarting the kernel to resolve potential issues with variable assignments, asking for specifics about the runtime error if problems persist.
  • A participant expresses gratitude for the assistance received and notes their ongoing learning process with Mathematica, comparing it to their experience with Matlab.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method for calculating the distance matrix, as multiple approaches are discussed, and some participants encounter different issues. The discussion remains open-ended with various suggestions and experiences shared.

Contextual Notes

Some limitations may arise from differences in Mathematica versions, as indicated by the mention of runtime errors and the need for kernel resets. The discussion does not resolve these issues but highlights the variability in user experiences.

Who May Find This Useful

Individuals learning Mathematica, particularly those transitioning from Matlab, as well as those interested in computational methods for distance calculations in programming environments.

brydustin
Messages
201
Reaction score
0
I wanted to make a matrix whose elements are the Eucledian distance between row vectors in another matrix, obviously this matrix will be symmetric because distance(x,y) = distance(y,x).
I have had no difficulty doing this in Matlab but I can't do it in Mathematica

Here is my attempt
A = {{1, 2, 3}, {3, 2, 1}, {1, 1, 1}};
B = IdentityMatrix[3]

For[i = 1, i < 4, i++,
For[j = i, j < 4, j++
B[[i, j]] = Norm[A[[j]] - A[]]
]
]
B = B + Transpose - DiagonalMatrix[Diagonal]

(*Where the last "trick" is done just so the loop is cut in half*)
Anyway, the loop part isn't working... any suggestions?
 
Physics news on Phys.org
Outer[yourfunction,yourmat,yourmat,1] will perform yourfunction on every pair of rows in yourmat.
Total[(#1-#2)^2]& will subtract two rows, square the result and then sum up all the squares.

In[17]:= X={{a,b,c},{d,e,f},{g,h,i}};
Outer[Total[(#1-#2)^2]&,X,X,1]

Out[18]={{0, (a-d)^2+(b-e)^2+(c-f)^2, (a-g)^2+(b-h)^2+(c-i)^2},
{(- a+d)^2+(-b+e)^2+(-c+f)^2, 0, (d-g)^2+(e-h)^2+(f-i)^2},
{(-a+g)^2+(-b+h)^2+(-c+i)^2, (-d+g)^2+(-e+h)^2+(-f+i)^2, 0}}

In[19]:=X={{1,2,3},{3,2,1},{1,1,1}};
Outer[Total[(#1-#2)^2]&,X,X,1]

Out[20]={{0,8,5},
{8,0,5},
{5,5,0}}
 
Last edited:
Bill Simpson said:
In[17]:= X={{a,b,c},{d,e,f},{g,h,i}};
Outer[Total[(#1-#2)^2]&,X,X,1]

Out[18]={{0, (a-d)^2+(b-e)^2+(c-f)^2, (a-g)^2+(b-h)^2+(c-i)^2},
{(- a+d)^2+(-b+e)^2+(-c+f)^2, 0, (d-g)^2+(e-h)^2+(f-i)^2},
{(-a+g)^2+(-b+h)^2+(-c+i)^2, (-d+g)^2+(-e+h)^2+(-f+i)^2, 0}}

In[19]:=X={{1,2,3},{3,2,1},{1,1,1}};
Outer[Total[(#1-#2)^2]&,X,X,1]

Out[20]={{0,8,5},
{8,0,5},
{5,5,0}}


That's funny, I got run time error when trying your code... which version are you using? I'm on 7.0.
What's the basic idea??
 
Refresh the page and read the couple of lines I edited in for explanation.

Then try killing the kernel and running it fresh to make sure you don't have some value assigned to something.

If that doesn't help then tell me exactly what the runtime error is.
 
Thanks! That did the trick!
Like I said in my intro, I'm learning mathematica, and to me its very strange compared to matlab. Don't be surprised if you see a few more posts on this website by me on this topic, I'm doing a project for work. Thanks again!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
7K