0
$\begingroup$

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?

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$
    – Syed
    Dec 6, 2021 at 17:53
  • $\begingroup$ @Syed I was thinking something like using $\{j,1,i-1\}$ for the second loop but that causes problems when $i=1$. $\endgroup$
    – Amarillo45
    Dec 6, 2021 at 17:59
  • $\begingroup$ (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. $\endgroup$
    – Syed
    Dec 6, 2021 at 18:06
  • $\begingroup$ @Syed But this includes dist[sequence[[1]],sequence[[1]]] in the list no? $\endgroup$
    – Amarillo45
    Dec 6, 2021 at 18:09
  • $\begingroup$ (g = Table[ a[i][j] = If[i != j, EuclideanDistance[sequence[[i]], sequence[[j]]]], {i, 1, 10}, {j, 1, i}]) // Grid $\endgroup$
    – Syed
    Dec 6, 2021 at 18:11

1 Answer 1

0
$\begingroup$

Response based on goals stated in comment:

n = 1000
seq = RandomReal[1, n];
distance = EuclideanDistance;
(* avoid i=j and duplicates *)
vals = Catenate@
   Table[distance[seq[[i]], seq[[j]]], {i, n}, {j, 1, i - 1}];
Histogram[vals]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.