Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Mr.Wizard Feb 2 '14 at 7:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 7 down vote accepted

Why not use Clip?

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

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

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

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

share|improve this answer

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}]
share|improve this answer

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