Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

marked as duplicate by Mr.Wizard Feb 2 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.

2 Answers

up vote 7 down vote accepted

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.

share|improve this answer
add comment

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
add comment

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