# Summation over diagonal blocks

I have generated a 12*12 matrix with

m = RandomInteger[{2, 5}, {12, 12}];


but I don't know how I can sum blocks on the main diagonal of the matrix such as:

It means the final results must be as

finalmatrix={{13,10,15},{13,11,15},{14,15,17}}

• Is your matrix always square, and is the dimension of the matrix always divisible by the block size and the number of blocks? Jul 31, 2016 at 15:02
• Yes the dimension of the matrix is divisible by blocks. Maybe 36*36 that should be 6 blocks of 6*6 and so on Jul 31, 2016 at 15:06
• You're going to have to set pretty specific rules if you want an answer. There's no obvious rule that says $12\times 12$ should be divided into $4^2$ blocks and $36\times 36$ in $6^2$ blocks. Jul 31, 2016 at 15:11
• @Feyre, unfortunately I cannot understand what you mean. 36*36=6*6*6*6*6*6 row and 6*6*6*6*6*6 column Jul 31, 2016 at 15:13
• I'm just saying the number of blocks you want to divide into between $12\times 12$ and $36\times 36$ seems arbitrary. Jul 31, 2016 at 15:15

ClearAll[blockPlus]
blockPlus = Tr[Partition[#, {#2, #2}], Plus, 2] &;

SeedRandom[1]
m = RandomInteger[{2, 5}, {12, 12}];
m // Grid[#, Dividers -> {#, #} &@Thread[Range[1, 13, 3] -> True]] &


blockPlus[m, 3]


{{14, 13, 10}, {14, 14, 12}, {12, 12, 11}}

blockPlus[m, 4]


{{12, 10, 11, 10}, {6, 9, 10, 9}, {12, 9, 6, 11}, {12, 10, 13, 12}}

blockPlus[m, 2]


{{18, 21}, {19, 21}}

• Besides so much thanks for your answer but I should ask that I have learnt for a function we must define such as f[x_]:=......... in blockPlus which is a function you did not define such as blockPlus[matrix_,n_] but at the below of that you put blockPlus[m,3] or so on! I cannot understand how this work can do Jul 31, 2016 at 15:33
• @Irr: blockPlus = Tr[Partition[#, {#2, #2}], Plus, 2] &; is equivalent to blockPlus[mat_, k_] := Tr[Partition[mat, {k, k}], Plus, 2]. Look up pure functions. Jul 31, 2016 at 15:35
• O my god, yes you used # and & it deals with pure functions. Jul 31, 2016 at 15:37

mat = RandomReal[{-1., 1.}, {10, 10}];


say you want to sum up diagonal 2 by 2 blocks

Total@Diagonal@Partition[mat, {2, 2}]

• Works though perhaps set up for {3,3} since that is what he asked. Jul 31, 2016 at 15:16

To add desired diagonal block of square matrix (condition for complete blocks):

mat[matr_, n_] :=
Total[matr[[#1 ;; #2, #1 ;; #2]] & @@@
NestList[# + n &, {1, n}, Length[matr[[1]]]/n - 1]] /;
IntegerQ[Length[matr[[1]]]/n]
mat[matr_, n_] := "incomplete"


"Cosmetics":

func[ma_, n_] :=
Grid[ma, Dividers -> Table[{{True}~Join~Table[False, n - 1]}, 2],
Background -> {None,
None, {##, ##} -> LightBlue & /@
NestList[# + n &, {1, n}, Length[ma[[1]]]/n - 1]}] /;
IntegerQ[Length[ma[[1]]]/n]
vis[matrix_, n_] :=
Row[{func[matrix, n] ->
Grid[mat[matrix, n], Frame -> True, Background -> LightBlue]}]


Test matrix:

RandomSeed[1];
m = RandomInteger[{2, 5}, {12, 12}];


Visualization:

Grid[Partition[vis[m, #] & /@ Divisors[12], 2], Alignment -> Left,
Frame -> All]