Question

I have a list of points where each point is a list of its 3 coordinates: x,y and z. But some of those points in their coordinates x and y are "bad" and I'd like to clean them. Is it possibile to write a single rule to do that? I've tried with:

cleanAdjustedPoints[adjustedPoints_List] := adjustedPoints /. {x_, y_, z_}  /; x < 0 -> {0, y, z}; /; y > constB -> {x, constB, z};

and I've seen that only the first rule is applied to the points with bad x, while the ones with bad y do not change. Mathematica does not give a sintax error so I thought that it was right. Any suggestions? thanks.

Was it helpful?

Solution

You just need to put the rules in a list. Also, note use of RuleDelayed (:>) which localises the variables x, y & z ensuring they don't pick up values from elsewhere in your program.

cleanAdjustedPoints[adjustedPoints_List] :=
  adjustedPoints /. {{x_, y_, z_} /; x < 0 :> {0, y, z},
    {x_, y_, z_} /; y > constB :> {x, constB, z}};

constB = 5;

cleanAdjustedPoints[{{-1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]

{{0, 2, 3}, {4, 5, 6}, {7, 5, 9}}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top