5
$\begingroup$

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:

Enter image description here

It means the final results must be as

finalmatrix={{13,10,15},{13,11,15},{14,15,17}}
$\endgroup$
  • $\begingroup$ Is your matrix always square, and is the dimension of the matrix always divisible by the block size and the number of blocks? $\endgroup$ – J. M. will be back soon Jul 31 '16 at 15:02
  • $\begingroup$ Yes the dimension of the matrix is divisible by blocks. Maybe 36*36 that should be 6 blocks of 6*6 and so on $\endgroup$ – Unbelievable Jul 31 '16 at 15:06
  • $\begingroup$ 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. $\endgroup$ – Feyre Jul 31 '16 at 15:11
  • $\begingroup$ @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 $\endgroup$ – Unbelievable Jul 31 '16 at 15:13
  • $\begingroup$ I'm just saying the number of blocks you want to divide into between $12\times 12$ and $36\times 36$ seems arbitrary. $\endgroup$ – Feyre Jul 31 '16 at 15:15
6
$\begingroup$
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]] &

Mathematica graphics

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}}

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Unbelievable Jul 31 '16 at 15:33
  • $\begingroup$ @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. $\endgroup$ – J. M. will be back soon Jul 31 '16 at 15:35
  • $\begingroup$ O my god, yes you used # and & it deals with pure functions. $\endgroup$ – Unbelievable Jul 31 '16 at 15:37
7
$\begingroup$

Perhaps Partition is helpful here?

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

say you want to sum up diagonal 2 by 2 blocks

Total@Diagonal@Partition[mat, {2, 2}]
$\endgroup$
  • 1
    $\begingroup$ Works though perhaps set up for {3,3} since that is what he asked. $\endgroup$ – Feyre Jul 31 '16 at 15:16
3
$\begingroup$

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]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.