Calculating Distance Matrix in Mathematica

In summary, In Mathematica, you cannot create a matrix whose elements are the Eucledian distance between row vectors. However, you can achieve this in Matlab using the Norm function.
  • #1
brydustin
205
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
  • #2
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:
  • #3
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??
 
  • #4
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.
 
  • #5
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!
 

Related to Calculating Distance Matrix in Mathematica

1. What is a distance matrix in Mathematica?

A distance matrix in Mathematica is a matrix that contains the distances between each pair of elements in a set of data. It is a useful tool for analyzing the relationships between data points, and can be used in various statistical and mathematical applications.

2. How do I create a distance matrix in Mathematica?

To create a distance matrix in Mathematica, you can use the built-in function "DistanceMatrix". This function takes in a list of data points and calculates the distances between each pair of points, resulting in a symmetric matrix with the distances as its elements.

3. Can I customize the distance metric used in a distance matrix?

Yes, you can customize the distance metric used in a distance matrix by specifying the "DistanceFunction" option in the "DistanceMatrix" function. This allows you to choose from a variety of distance metrics such as Euclidean, Manhattan, and Cosine distances.

4. How can I visualize a distance matrix in Mathematica?

You can visualize a distance matrix in Mathematica by using the "MatrixPlot" function. This will display the matrix as a color-coded grid, with darker colors representing larger distances and lighter colors representing smaller distances.

5. Can I use a distance matrix in machine learning and data analysis?

Yes, distance matrices are commonly used in machine learning and data analysis tasks such as clustering, classification, and dimensionality reduction. They can also be used in network analysis and image processing applications.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
8
Views
903
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
961
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
Back
Top