2

I want to generate a table of Euclidean distances from the points in a list to a given point, e.g.:

Table[EuclideanDistance[MyList[[i]], c], {i, 1, Length[MyList]}]

Moreover, when EuclideanDistance returns a value greater than $T$, I'd like to replace that value with a given real number $R$. Is there a simple way to do this?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
SnowTrace
  • 149
  • 5

2 Answers2

7

Why not use Clip?

MyList = RandomReal[{-1, 1}, 10];
c = {0, 0};
t = 1;
r = -1;

Clip[
 Table[EuclideanDistance[MyList[[i]], c], {i, 1, Length[MyList]}],
 {0, 1}, {0, r}]

(* ==>  {0.9957995322104205`,0.3452581732209688`,0.016464628727136405`,
  -1,-1,0.5914902487316964`,0.8531216593853862`,-1,0.9996775567985703`,
  -1} *)

Here, r = -1 is the value that replaces any number above the threshold t = 1.

Jens
  • 97,245
  • 7
  • 213
  • 499
0

Here's a variation on Jens method that does the calculation by mapping the function EuclideanDistance onto the list and then clipping the result:

myList = RandomReal[{-1, 1}, 10];
c = {0, 0}; t = 1; r = -1;
Clip[EuclideanDistance[#, c] & /@ myList, {0, 1}, {0, r}]
bill s
  • 68,936
  • 4
  • 101
  • 191