# Table over two variables but ignore terms with common indices

I have a list of 1000 numbers called “sequence” and a function “dist” that calculates the distance between two numbers.

I am trying to create a table of distances between terms, but ignoring the terms

dist[sequence[[i]],sequence[[i]]]


for all $$i$$.

How can I change the code

Table[dist[sequence[[i]],sequence[[j]]],{i,1,1000},{j,1,1000}]


In order to ignore the distances between a term and itself?

• Welcome to the Mathematica Stack Exchange. You realize that the distance between points i and j will also be similar, whether it is (i,j) or (j,i) pair. As a (smaller example) try this: sequence = RandomReal[{1, 10}, 10]; and Table[a[i][j] = EuclideanDistance[sequence[[i]], sequence[[j]]], {i, 1, 10}, {j, 1, 10}] // Grid. Try a[3][2] and a[2][3] : Substitute dist in this example.
– Syed
Dec 6, 2021 at 17:53
• @Syed I was thinking something like using $\{j,1,i-1\}$ for the second loop but that causes problems when $i=1$. Dec 6, 2021 at 17:59
• (p = Table[ a[i][j] = EuclideanDistance[sequence[[i]], sequence[[j]]], {i, 1, 10}, {j, 1, i - 1}]) // Grid works ok. Also you can take a look at SparseArray since you matrix will be half-filled and has 1000 x 1000 entries.
– Syed
Dec 6, 2021 at 18:06
• @Syed But this includes dist[sequence[[1]],sequence[[1]]] in the list no? Dec 6, 2021 at 18:09
• (g = Table[ a[i][j] = If[i != j, EuclideanDistance[sequence[[i]], sequence[[j]]]], {i, 1, 10}, {j, 1, i}]) // Grid
– Syed
Dec 6, 2021 at 18:11

n = 1000