# In a list having triplet elements, how can I change the third element of a triplet when it has a given value? [duplicate]

list = {{120.563, 51.0257, 880}, {120.302, 51.0257, 840}, {120.041, 51.0257,
840}, {119.78, 51.0257, 790}, {119.519, 51.0257, 600}, {119.257,
51.0257, 640}, {118.996, 51.0257, 880}, {118.735, 51.0257, 810},
{118.474, 51.0257, 1130}, {118.213, 51.0257, 840}, {117.952, 51.0257,
870}, {117.69, 51.0257, 950}, {117.429, 51.0257, 930}, {117.168,
51.0257, 930}, {116.907, 51.0257, 1180}, {116.646, 51.0257, 1040},
{116.384, 51.0257, 870}, {116.123, 51.0257, 760}, {115.862, 51.0257,
910}, {115.601, 51.0257, 650}, {115.34, 51.0257, 650}, {115.079,
51.0257, 660}, {114.817, 51.0257, 800}, {114.556, 51.0257, 780},
{114.295, 51.0257, 720}, {114.034, 51.0257, 950}, {113.773, 51.0257,
1040}, {113.511, 51.0257, 960}, {113.25, 51.0257, 1190}, {112.989,
51.0257, 1130}, {112.728, 51.0257, 1060}, {112.467, 51.0257, 1230},
{112.206, 51.0257, 1150}, {111.944, 51.0257, 790}, {111.683, 51.0257,
910}, {111.422, 51.0257, 940}, {111.161, 51.0257, 1090}, {110.9,
51.0257, 1160}, {110.638, 51.0257, 900}, {110.377, 51.0257, 1190},
{110.116, 51.0257, 1110}, {109.855, 51.0257, 1210}, {109.594,
51.0257, 1250}, {109.332, 51.0257, 1280}, {109.071, 51.0257, 780},
{108.81, 51.0257, 910}, {108.549, 51.0257, 990}, {108.288, 51.0257,
1100}, {108.027, 51.0257, 1060}, {107.765, 51.0257, 700}, {107.504,
51.0257, 650}, {107.243, 51.0257, 760}, {106.982, 51.0257, 680},
{106.721, 51.0257, 660}, {106.459, 51.0257, 580}, {106.198, 51.0257,
580}, {105.937, 51.0257, 820}, {105.676, 51.0257, 1110}, {105.415,
51.0257, 1370}, {105.154, 51.0257, 1590}, {104.892, 51.0257, 1600},
{104.631, 51.0257, 1360}, {104.37, 51.0257, 1450}, {104.109, 51.0257,
1090}, {103.848, 51.0257, 1570}, {103.586, 51.0257, 1990}, {103.325,
51.0257, 1950}, {103.064, 51.0257, 1840}, {102.803, 51.0257, 1700},
{102.542, 51.0257, 1840}, {102.281, 51.0257, 1840}, {102.019,
51.0257, 2120}, {101.758, 51.0257, 1860}, {101.497, 51.0257, 1510},
{101.236, 51.0257, 1690}, {100.975, 51.0257, 2060}, {100.713,
51.0257, 1720}, {100.452, 51.0257, 1640}, {100.191, 51.0257, 1940}}


If a sublist has third value = 1000, I want to change it to 300.

• tnx... this is same value ... my list 14641 ... – BatbuyanMGL May 2 '14 at 8:09
• @Artes The question you've linked has is condition-less case of this one. So I do not agree it's a duplicate, solutions here and there are just two sets with common part. But complements are not empty. – Kuba May 2 '14 at 12:45
• @Kuba I'm still not convinced it isn't. I close such questions in case they are of rather low quality. However sometimes well posed questions deserve to be open even if they are more similar to another ones . I think it is not the case here. Nonetheless you you can vote to reopen it. E.g. I voted to close this question but subsequently decided to retract my close vote because it was in fact original one (after merging it with his another question). – Artes May 2 '14 at 14:30

Let's say it is your matrix, where you want to replace not 300s but 1s.

list = Table[{RandomReal[], RandomReal[], RandomInteger[{1, 3}]}
, {10}]

 {{0.08796, 0.0203691, 1}, {0.222052, 0.453192, 1}, {0.610724, 0.804157, 3},
{0.362402, 0.624669, 2}, {0.344305, 0.0202516, 1}, {0.213401, 0.34757, 1},
{0.336219, 0.773813, 1}, {0.777852, 0.413674, 3}, {0.0869701, 0.663478, 3},
{0.921255, 0.694108, 2}}


You can do:

list /. {x__, 1} :> {x, 15}


But it will be useless if you don't know if there are Integers or Reals in the 3rd column. Take a look a relevant: 15909. So quick fix is:

list /. {x__, y_} :> {x, 15} /; y == 1
(*and it will work on the case below too while the first solution wasn't*)
N @ list /. {x__, y_} :> {x, 15} /; y == 1


Alternatively you can use MapAt with Span (for V9+ only):

MapAt[If[# == 1, 15, #] &, list, {All, 3}]


or

If[#3 == 1, {#, #2, 15}, {##}] & @@@ list