- #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?
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?