Challenge
Given a matrix M with r rows and c columns, and two Boolean lists V of length r and H of length c, calculate the partitioned cumulative vertical and horizontal sums.
Rules
r and c are greater than or equal to one
H and V begin with a true value
The values in M are within your language's reasonable numeric domain.
Partitioning and summing begins in the top left corner.
Walk through
Given M:
┌──────────────┐ │ 1 2 3 4 5│ │ 6 7 8 9 10│ │11 12 13 14 15│ │16 17 18 19 20│ └──────────────┘ H: 1 0 1 0 0
V: 1 1 0 1
Split M into groups of columns, beginning a new group at every true value of H
┌─────┬────────┐ │ 1 2│ 3 4 5│ │ 6 7│ 8 9 10│ │11 12│13 14 15│ │16 17│18 19 20│ └─────┴────────┘ Split each group of columns into groups of rows, beginning a new group at every true value of V:
┌─────┬────────┐ │ 1 2│ 3 4 5│ ├─────┼────────┤ │ 6 7│ 8 9 10│ │11 12│13 14 15│ ├─────┼────────┤ │16 17│18 19 20│ └─────┴────────┘ Cumulatively sum each cell horizontally:
┌─────┬────────┐ │ 1 3│ 3 7 12│ ├─────┼────────┤ │ 6 13│ 8 17 27│ │11 23│13 27 42│ ├─────┼────────┤ │16 33│18 37 57│ └─────┴────────┘ Cumulatively sum each cell vertically:
┌─────┬────────┐ │ 1 3│ 3 7 12│ ├─────┼────────┤ │ 6 13│ 8 17 27│ │17 36│21 44 69│ ├─────┼────────┤ │16 33│18 37 57│ └─────┴────────┘ Result:
┌──────────────┐ │ 1 3 3 7 12│ │ 6 13 8 17 27│ │17 36 21 44 69│ │16 33 18 37 57│ └──────────────┘ Additional test cases
M:
┌───────────┐ │15 11 11 17│ │13 20 18 8│ └───────────┘ H: 1 0 0 1 V: 1 0
Result:
┌───────────┐ │15 26 37 17│ │28 59 88 25│ └───────────┘ M:
┌─┐ │7│ └─┘ Result (H and V must be 1):
┌─┐ │7│ └─┘ M:
┌──┐ │ 3│ │-1│ │ 4│ └──┘ V: 1 1 0 (H must be 1)
Result:
┌──┐ │ 3│ │-1│ │ 3│ └──┘ M:
┌───────────────────────────────────────────────────────┐ │10 7.7 1.9 1.5 5.4 1.2 7.8 0.6 4.3 1.2 4.5 5.4 0.3│ │ 2.3 3.8 4.1 4.5 1 7.7 3 3.4 6.9 5.8 9.5 1.3 7.5│ │ 9.1 3.7 7.2 9.8 3.9 10 7.6 9.6 7.3 6.2 3.3 9.2 9.4│ │ 4.3 4.9 7.6 2 1.4 5.8 8.1 2.4 1.1 2.3 7.3 3.6 6 │ │ 9.3 10 5.8 9.6 5.7 8.1 2.1 3.9 4 1.3 6.3 3.1 9 │ │ 6.6 1.4 0.5 6.5 4.6 2.1 7.5 4.3 9 7.2 2.8 3.6 4.6│ │ 1.7 9.9 2.4 4.5 1.3 2.6 6.4 7.8 6.2 3.2 10 5.2 8.9│ │ 9.9 5.3 4.5 6.3 1.4 3.1 2.3 7.9 7.8 7.9 9.6 4 5.8│ └───────────────────────────────────────────────────────┘ H: 1 0 0 1 0 1 1 1 0 1 1 1 0
V: 1 0 0 0 0 1 0 0
Result:
┌────────────────────────────────────────────────────────────────┐ │10 17.7 19.6 1.5 6.9 1.2 7.8 0.6 4.9 1.2 4.5 5.4 5.7│ │12.3 23.8 29.8 6 12.4 8.9 10.8 4 15.2 7 14 6.7 14.5│ │21.4 36.6 49.8 15.8 26.1 18.9 18.4 13.6 32.1 13.2 17.3 15.9 33.1│ │25.7 45.8 66.6 17.8 29.5 24.7 26.5 16 35.6 15.5 24.6 19.5 42.7│ │35 65.1 91.7 27.4 44.8 32.8 28.6 19.9 43.5 16.8 30.9 22.6 54.8│ │ 6.6 8 8.5 6.5 11.1 2.1 7.5 4.3 13.3 7.2 2.8 3.6 8.2│ │ 8.3 19.6 22.5 11 16.9 4.7 13.9 12.1 27.3 10.4 12.8 8.8 22.3│ │18.2 34.8 42.2 17.3 24.6 7.8 16.2 20 43 18.3 22.4 12.8 32.1│ └────────────────────────────────────────────────────────────────┘