Create a multiplier vector and an addition vector from the condition masks.
n⌷data
gets you the n
th row a data
matrix.
The columns of the second row that you want to double are indicated by:
3=3⌷data
1 0 1 0
So the multiplication vector is:
1+3=3⌷data
2 1 2 1
The columns of the second row that you want to add 3 to are:
1=3⌷data
0 1 0 0
So the addition vector is:
3×1=3⌷data
0 3 0 0
The new second row is thus:
(3×1=3⌷data)+(1+3=3⌷data)×2⌷data
10 12 12 0
We can express this as a dyadic function taking the 3rd row as left argument (the control) and the second row as right argument (the actual data):
Update←{(3×1=⍺)+(1+3=⍺)×⍵}
(3⌷data) Update (2⌷data)
10 12 12 0
Now we can either create a new matrix with the updated values:
(3⌷data) Update@2 ⊢data
a b c d
10 12 12 0
3 1 3 2
Or do the replacement in-place:
(2⌷data) Update⍨← (3⌷data)
data
a b c d
10 12 12 0
3 1 3 2
Try it online!
Note that your algorithms would be simplified and your code run faster if you kept data with different roles in separate variables. For example:
(keys values control)←↓data
control Update values
10 12 12 0
values Update⍨← control
values
10 12 12 0
↑keys values control
a b c d
10 12 12 0
3 1 3 2
Try it online!
The sum of the increases is simply the sum of the differences between the new values and the original values:
+/values-⍨control Update values
14
Try it online!