# How can I change entries in a list of lists depending on the entries in the list itself?

I have an 3D dimensional Array of data. It has this kind of strucuture: {i,x,y}

{{1, 1, 6}, {2, 2, 4}, {3, 3, 5}, {4, 4, 7}, {5, 5, 2}, {6, 6, 5}, {7,
8, 2}, {8, 9, 7}, {9, 10, 6}, {10, 13, 6}, {11, 14, 7}, {12, 15,
3}, {13, 16, 6}, {14, 18, 3}, {15, 21, 1}, {16, 22, 3}, {17, 23,
4}, {18, 24, 2}, {19, 26, 4}, {20, 28, 6}, {21, 29, 6}, {22, 31,
2}, {23, 33, 5}, {24, 34, 5}, {25, 35, 5}, {26, 36, 5}, {27, 38,
3}, {28, 40, 7}}


I want to change only the w column of the data in this way: Lets say we are at the i´th row: {i,v,w} and the next is the i+1 row: {i+1,y,z} The rule should be:

   If v+w>=y then w->y - 1 - v Else nothing


And for the last row, where there isnt any y the y should be v1+n. The very first v1 plus some kind of a number. Meaning the last v->y1+n - 1 - v This is very confusing so I will give an example: Lets say the starting array is:

   {{1,5,3},{2,7,4},{3,8,0},{4,10,3},{5,14,7}} n=15;


Then the resulting array would be:

   {{1,5,1},{2,7,0},{3,8,0},{4,10,3},{5,14,5}}


And the second part of the problem would be only changing the v column:

   v->v+w

• Check the document of BlockMap. Jul 23, 2023 at 11:05
• @xzczd this doesnt work for the last element of the list. Also BlockMap unfortunately doesnt process "If" functions :C Jul 23, 2023 at 12:04
• Just append an extra element at the end and remove it afterwards, or use MapAt to handle the last element. "Also BlockMap unfortunately doesnt process "If" functions" I don't think so. How do you use it? Jul 23, 2023 at 12:12
• If you don't know how to define function in Mathematica, start from reference.wolfram.com/language/tutorial/… Also, your blax, bla(x+1), blay notation is just terrible, please improve it. Jul 23, 2023 at 12:19
• It should be f[{{u_,v_,w_},{x_,y_,z_}}] Jul 23, 2023 at 13:05

Here's a helper function:

f[block : {{_, a2_, a3_}, {_, b2_, _}}] :=
If[a2 + a3 >= b2, ReplacePart[block[[1]], 3 -> b2 - 1 - a2], block[[1]]]


We can use this with BlockMap, but we need to append the dummy value:

BlockMap[f, Append[test, {0, test[[1, 2]] + Length[Flatten[test]], 0}], 2, 1]


where test is

test = {{1, 5, 3}, {2, 7, 4}, {3, 8, 0}, {4, 10, 3}, {5, 14, 7}}